From b372ff212a4b42f4ac857a15e95bf04531fa260d Mon Sep 17 00:00:00 2001 From: molivier Date: Thu, 2 Oct 2003 09:04:12 +0000 Subject: [PATCH] The crusade against buffer overflows continues... git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@3524 d7cf8633-e32d-0410-b094-e92efae38249 --- cl_particles.c | 4 ++-- common.c | 23 ++++++++++++++++------- common.h | 5 ++--- fs.c | 9 +++++++-- fs.h | 2 +- model_brush.c | 12 ++++++------ r_shadow.c | 12 ++++++------ 7 files changed, 40 insertions(+), 27 deletions(-) diff --git a/cl_particles.c b/cl_particles.c index 9f09eeec..59b745fb 100644 --- a/cl_particles.c +++ b/cl_particles.c @@ -509,8 +509,8 @@ void CL_ReadPointFile_f (void) if (!cl.worldmodel) return; - FS_StripExtension(cl.worldmodel->name, name); - strcat(name, ".pts"); + FS_StripExtension (cl.worldmodel->name, name, sizeof (name)); + strlcat (name, ".pts", sizeof (name)); #if WORKINGLQUAKE pointfile = COM_LoadTempFile (name); #else diff --git a/common.c b/common.c index 7dab60b7..e8c0893f 100644 --- a/common.c +++ b/common.c @@ -675,8 +675,8 @@ void COM_InitArgv (void) void COM_InitGameType (void) { char name[MAX_OSPATH]; - FS_StripExtension(com_argv[0], name); - COM_ToLowerString(name, name); + FS_StripExtension (com_argv[0], name, sizeof (name)); + COM_ToLowerString (name, name, sizeof (name)); if (strstr(name, "transfusion")) gamemode = GAME_TRANSFUSION; @@ -843,28 +843,37 @@ char *va(const char *format, ...) //====================================== -// LordHavoc: added these because they are useful -void COM_ToLowerString(const char *in, char *out) +void COM_ToLowerString (const char *in, char *out, size_t size_out) { - while (*in) + if (size_out == 0) + return; + + while (*in && size_out > 1) { if (*in >= 'A' && *in <= 'Z') *out++ = *in++ + 'a' - 'A'; else *out++ = *in++; + size_out--; } + *out = '\0'; } -void COM_ToUpperString(const char *in, char *out) +void COM_ToUpperString (const char *in, char *out, size_t size_out) { - while (*in) + if (size_out == 0) + return; + + while (*in && size_out > 1) { if (*in >= 'a' && *in <= 'z') *out++ = *in++ + 'A' - 'a'; else *out++ = *in++; + size_out--; } + *out = '\0'; } int COM_StringBeginsWith(const char *s, const char *match) diff --git a/common.h b/common.h index 5e3a0bd0..832561a2 100644 --- a/common.h +++ b/common.h @@ -176,9 +176,8 @@ extern char *gamename; extern char *gamedirname; extern char com_modname[MAX_OSPATH]; -// LordHavoc: useful... -void COM_ToLowerString(const char *in, char *out); -void COM_ToUpperString(const char *in, char *out); +void COM_ToLowerString (const char *in, char *out, size_t size_out); +void COM_ToUpperString (const char *in, char *out, size_t size_out); int COM_StringBeginsWith(const char *s, const char *match); typedef struct stringlist_s diff --git a/fs.c b/fs.c index 7fe9c39a..bbdaf2f6 100644 --- a/fs.c +++ b/fs.c @@ -1635,16 +1635,21 @@ OTHERS PUBLIC FUNCTIONS FS_StripExtension ============ */ -void FS_StripExtension (const char *in, char *out) +void FS_StripExtension (const char *in, char *out, size_t size_out) { char *last = NULL; - while (*in) + + if (size_out == 0) + return; + + while (*in && size_out > 1) { if (*in == '.') last = out; else if (*in == '/' || *in == '\\' || *in == ':') last = NULL; *out++ = *in++; + size_out--; } if (last) *last = 0; diff --git a/fs.h b/fs.h index f41a6f1c..43a16837 100644 --- a/fs.h +++ b/fs.h @@ -63,7 +63,7 @@ qboolean FS_WriteFile (const char *filename, void *data, int len); // ------ Other functions ------ // -void FS_StripExtension (const char *in, char *out); +void FS_StripExtension (const char *in, char *out, size_t size_out); void FS_DefaultExtension (char *path, const char *extension, size_t size_path); qboolean FS_FileExists (const char *filename); // the file can be into a package diff --git a/model_brush.c b/model_brush.c index 55a33a53..52c6fb58 100644 --- a/model_brush.c +++ b/model_brush.c @@ -1038,9 +1038,9 @@ static void Mod_Q1BSP_LoadLighting(lump_t *l) else // LordHavoc: bsp version 29 (normal white lighting) { // LordHavoc: hope is not lost yet, check for a .lit file to load - strcpy(litfilename, loadmodel->name); - FS_StripExtension(litfilename, litfilename); - strcat(litfilename, ".lit"); + strlcpy (litfilename, loadmodel->name, sizeof (litfilename)); + FS_StripExtension (litfilename, litfilename, sizeof (litfilename)); + strlcat (litfilename, ".lit", sizeof (litfilename)); data = (qbyte*) FS_LoadFile(litfilename, false); if (data) { @@ -1093,9 +1093,9 @@ static void Mod_Q1BSP_LoadLightList(void) char lightsfilename[1024], *s, *t, *lightsstring; mlight_t *e; - strcpy(lightsfilename, loadmodel->name); - FS_StripExtension(lightsfilename, lightsfilename); - strcat(lightsfilename, ".lights"); + strlcpy (lightsfilename, loadmodel->name, sizeof (lightsfilename)); + FS_StripExtension (lightsfilename, lightsfilename, sizeof(lightsfilename)); + strlcat (lightsfilename, ".lights", sizeof (lightsfilename)); s = lightsstring = (char *) FS_LoadFile(lightsfilename, false); if (s) { diff --git a/r_shadow.c b/r_shadow.c index 52e4f350..8a05dfa4 100644 --- a/r_shadow.c +++ b/r_shadow.c @@ -1981,8 +1981,8 @@ void R_Shadow_LoadWorldLights(void) Con_Printf("No map loaded.\n"); return; } - FS_StripExtension(cl.worldmodel->name, name); - strcat(name, ".rtlights"); + FS_StripExtension (cl.worldmodel->name, name, sizeof (name)); + strlcat (name, ".rtlights", sizeof (name)); lightsstring = FS_LoadFile(name, false); if (lightsstring) { @@ -2038,8 +2038,8 @@ void R_Shadow_SaveWorldLights(void) Con_Printf("No map loaded.\n"); return; } - FS_StripExtension(cl.worldmodel->name, name); - strcat(name, ".rtlights"); + FS_StripExtension (cl.worldmodel->name, name, sizeof (name)); + strlcat (name, ".rtlights", sizeof (name)); bufchars = bufmaxchars = 0; buf = NULL; for (light = r_shadow_worldlightchain;light;light = light->next) @@ -2079,8 +2079,8 @@ void R_Shadow_LoadLightsFile(void) Con_Printf("No map loaded.\n"); return; } - FS_StripExtension(cl.worldmodel->name, name); - strcat(name, ".lights"); + FS_StripExtension (cl.worldmodel->name, name, sizeof (name)); + strlcat (name, ".lights", sizeof (name)); lightsstring = FS_LoadFile(name, false); if (lightsstring) { -- 2.39.2