diff --git a/src/refresh/files/jpeg.c b/src/refresh/files/jpeg.c index 7cbe210b..e73bb0bd 100644 --- a/src/refresh/files/jpeg.c +++ b/src/refresh/files/jpeg.c @@ -85,17 +85,14 @@ LoadJPG(char *origname, byte **pic, int *width, int *height) byte *rawdata, *rgbadata, *scanline, *p, *q; unsigned int rawsize, i; - /* Add the extension */ - len = strlen(origname); + strncpy(filename, origname, sizeof(filename) - 1); + filename[sizeof(filename) - 1] = '\0'; - if (strcmp(origname + len - 4, ".jpg")) + /* Add the extension */ + len = strlen(filename); + if (strcmp(filename + len - 4, ".jpg")) { - strncpy(filename, origname, 256); - strncat(filename, ".jpg", 255); - } - else - { - strncpy(filename, origname, 256); + strncat(filename, ".jpg", sizeof(filename) - 1 - len); } *pic = NULL; diff --git a/src/refresh/files/pcx.c b/src/refresh/files/pcx.c index d1e9b3fc..f1c9062f 100644 --- a/src/refresh/files/pcx.c +++ b/src/refresh/files/pcx.c @@ -38,17 +38,14 @@ LoadPCX(char *origname, byte **pic, byte **palette, int *width, int *height) byte *out, *pix; char filename[256]; + strncpy(filename, origname, sizeof(filename) - 1); + filename[sizeof(filename) - 1] = '\0'; + /* Add the extension */ filelen = strlen(origname); - - if (strcmp(origname + filelen - 4, ".pcx")) + if (strcmp(filename + filelen - 4, ".pcx")) { - strncpy(filename, origname, 256); - strncat(filename, ".pcx", 255); - } - else - { - strncpy(filename, origname, 256); + strncat(filename, ".pcx", sizeof(filename) - 1 - filelen); } *pic = NULL; diff --git a/src/refresh/files/tga.c b/src/refresh/files/tga.c index 20783aa2..b6c46d8d 100644 --- a/src/refresh/files/tga.c +++ b/src/refresh/files/tga.c @@ -50,17 +50,14 @@ LoadTGA(char *origname, byte **pic, int *width, int *height) char name[256]; int len; + strncpy(name, origname, sizeof(name) - 1); + name[sizeof(name) - 1] = '\0'; + /* Add the extension */ len = strlen(origname); - - if (strcmp(origname + len - 4, ".tga")) + if (strcmp(name + len - 4, ".tga")) { - strncpy(name, origname, 256); - strncat(name, ".tga", 255); - } - else - { - strncpy(name, origname, 256); + strncat(name, ".tga", sizeof(name) - 1 - len); } *pic = NULL; diff --git a/src/refresh/files/wal.c b/src/refresh/files/wal.c index 657acec8..48d341ef 100644 --- a/src/refresh/files/wal.c +++ b/src/refresh/files/wal.c @@ -35,17 +35,14 @@ LoadWal(char *origname) int len; char name[256]; - /* Add the extension */ - len = strlen(origname); + strncpy(name, origname, sizeof(name) - 1); + name[sizeof(name) - 1] = '\0'; - if (strcmp(origname + len - 4, ".wal")) + /* Add the extension */ + len = strlen(name); + if (strcmp(name + len - 4, ".wal")) { - strncpy(name, origname, 256); - strncat(name, ".wal", 255); - } - else - { - strncpy(name, origname, 256); + strncat(name, ".wal", sizeof(name) - 1 - len); } ri.FS_LoadFile(name, (void **)&mt);