diff --git a/polymer/eduke32/build/include/compat.h b/polymer/eduke32/build/include/compat.h index add4e02fa..d03b0cc76 100644 --- a/polymer/eduke32/build/include/compat.h +++ b/polymer/eduke32/build/include/compat.h @@ -675,6 +675,7 @@ char *Bstrlwr(char *); char *Bstrupr(char *); #endif +// Copy min(strlen(src)+1, n) characters into dst, always terminate with a NUL. static inline char *Bstrncpyz(char *dst, const char *src, bsize_t n) { Bstrncpy(dst, src, n); @@ -682,6 +683,19 @@ static inline char *Bstrncpyz(char *dst, const char *src, bsize_t n) return dst; } +// Append extension when contains no dot. +// should be like ".mhk", i.e. it MUST start with a dot. +// The ugly name is deliberate: we should be checking the sizes of all buffers! +static inline void append_ext_UNSAFE(char *outbuf, const char *ext) +{ + char *p = Bstrrchr(outbuf,'.'); + + if (!p) + Bstrcat(outbuf, ext); + else + Bstrcpy(p+1, ext+1); +} + #ifdef EXTERNC } #endif diff --git a/polymer/eduke32/build/src/build.c b/polymer/eduke32/build/src/build.c index b9e212bfe..feacc55f9 100644 --- a/polymer/eduke32/build/src/build.c +++ b/polymer/eduke32/build/src/build.c @@ -822,22 +822,13 @@ CANCEL: static int32_t mhk=0; static void loadmhk(int32_t domessage) { - char *p; char levname[BMAX_PATH]; + char levname[BMAX_PATH]; if (!mhk) return; Bstrcpy(levname, boardfilename); - p = Bstrrchr(levname,'.'); - if (!p) - Bstrcat(levname,".mhk"); - else - { - p[1]='m'; - p[2]='h'; - p[3]='k'; - p[4]=0; - } + append_ext_UNSAFE(levname, ".mhk"); if (!loadmaphack(levname)) { diff --git a/polymer/eduke32/build/src/engine.c b/polymer/eduke32/build/src/engine.c index c609a968e..79b7235d6 100644 --- a/polymer/eduke32/build/src/engine.c +++ b/polymer/eduke32/build/src/engine.c @@ -9932,21 +9932,10 @@ int32_t loadboard(char *filename, char flags, vec3_t *dapos, int16_t *daang, int #endif { - char *p, fn[BMAX_PATH]; + char fn[BMAX_PATH]; Bstrcpy(fn, filename); - - p = Bstrrchr(fn, '.'); - - if (!p) - Bstrcat(fn, ".cfg"); - else - { - p[1]='c'; - p[2]='f'; - p[3]='g'; - p[4]=0; - } + append_ext_UNSAFE(fn, ".cfg"); OSD_Exec(fn); } diff --git a/polymer/eduke32/source/premap.c b/polymer/eduke32/source/premap.c index b270133cf..cbd0df2da 100644 --- a/polymer/eduke32/source/premap.c +++ b/polymer/eduke32/source/premap.c @@ -1759,16 +1759,7 @@ static void G_LoadMapHack(char *outbuf, const char *filename) if (filename != NULL) Bstrcpy(outbuf, filename); - p = Bstrrchr(outbuf,'.'); - if (!p) - Bstrcat(outbuf,".mhk"); - else - { - p[1]='m'; - p[2]='h'; - p[3]='k'; - p[4]=0; - } + append_ext_UNSAFE(outbuf, ".mhk"); if (!loadmaphack(outbuf)) initprintf("Loaded map hack file \"%s\"\n",outbuf); diff --git a/polymer/eduke32/source/savegame.c b/polymer/eduke32/source/savegame.c index 23bac7aa6..a274e63e3 100644 --- a/polymer/eduke32/source/savegame.c +++ b/polymer/eduke32/source/savegame.c @@ -285,18 +285,7 @@ int32_t G_LoadPlayer(int32_t spot) // TODO: this stuff needs to be factored out, too... if (currentboardfilename[0]) { - char *p; - - p = Bstrrchr(currentboardfilename,'.'); - if (!p) Bstrcat(currentboardfilename,".mhk"); - else - { - p[1]='m'; - p[2]='h'; - p[3]='k'; - p[4]=0; - } - + append_ext_UNSAFE(currentboardfilename, ".mhk"); loadmaphack(currentboardfilename); }