doom: Eliminate use of unsafe string functions.

Eliminate use of strcpy, strcat, strncpy, and use the new safe
alternatives.
This commit is contained in:
Simon Howard 2014-03-29 21:23:29 -04:00
parent e76b5678bf
commit 040ca1cfb5
7 changed files with 90 additions and 69 deletions

View file

@ -659,21 +659,27 @@ static char *GetGameName(char *gamename)
if (deh_sub != banners[i])
{
size_t gamename_size;
int version;
// Has been replaced.
// We need to expand via printf to include the Doom version number
// We also need to cut off spaces to get the basic name
gamename = Z_Malloc(strlen(deh_sub) + 10, PU_STATIC, 0);
gamename_size = strlen(deh_sub) + 10;
gamename = Z_Malloc(gamename_size, PU_STATIC, 0);
version = G_VanillaVersionCode();
sprintf(gamename, deh_sub, version / 100, version % 100);
while (gamename[0] != '\0' && isspace(gamename[0]))
strcpy(gamename, gamename+1);
{
memmove(gamename, gamename + 1, gamename_size - 1);
}
while (gamename[0] != '\0' && isspace(gamename[strlen(gamename)-1]))
{
gamename[strlen(gamename) - 1] = '\0';
}
return gamename;
}

View file

@ -32,6 +32,7 @@
#include "d_main.h"
#include "m_argv.h"
#include "m_menu.h"
#include "m_misc.h"
#include "i_system.h"
#include "i_timer.h"
#include "i_video.h"
@ -59,8 +60,8 @@ static void PlayerQuitGame(player_t *player)
// Do this the same way as Vanilla Doom does, to allow dehacked
// replacements of this message
strncpy(exitmsg, DEH_String("Player 1 left the game"), sizeof(exitmsg));
exitmsg[sizeof(exitmsg) - 1] = '\0';
M_StringCopy(exitmsg, DEH_String("Player 1 left the game"),
sizeof(exitmsg));
exitmsg[7] += player_num;

View file

@ -955,7 +955,11 @@ void G_Ticker (void)
case BTS_SAVEGAME:
if (!savedescription[0])
strcpy (savedescription, "NET GAME");
{
M_StringCopy(savedescription, "NET GAME",
sizeof(savedescription));
}
savegameslot =
(players[i].cmd.buttons & BTS_SAVEMASK)>>BTS_SAVESHIFT;
gameaction = ga_savegame;
@ -1512,7 +1516,7 @@ char savename[256];
void G_LoadGame (char* name)
{
strcpy (savename, name);
M_StringCopy(savename, name, sizeof(savename));
gameaction = ga_loadgame;
}
@ -1577,7 +1581,7 @@ G_SaveGame
char* description )
{
savegameslot = slot;
strcpy (savedescription, description);
M_StringCopy(savedescription, description, sizeof(savedescription));
sendsave = true;
}
@ -1631,7 +1635,7 @@ void G_DoSaveGame (void)
rename(temp_savegame_file, savegame_file);
gameaction = ga_nothing;
strcpy(savedescription, "");
M_StringCopy(savedescription, "", sizeof(savedescription));
players[consoleplayer].message = DEH_String(GGSAVED);

View file

@ -38,6 +38,7 @@
#include "hu_stuff.h"
#include "hu_lib.h"
#include "m_controls.h"
#include "m_misc.h"
#include "w_wad.h"
#include "s_sound.h"
@ -612,7 +613,7 @@ boolean HU_Responder(event_t *ev)
// leave chat mode and notify that it was sent
chat_on = false;
strcpy(lastmessage, chat_macros[c]);
M_StringCopy(lastmessage, chat_macros[c], sizeof(lastmessage));
plr->message = lastmessage;
eatkey = true;
}
@ -634,7 +635,7 @@ boolean HU_Responder(event_t *ev)
chat_on = false;
if (w_chat.l.len)
{
strcpy(lastmessage, w_chat.l.l);
M_StringCopy(lastmessage, w_chat.l.l, sizeof(lastmessage));
plr->message = lastmessage;
}
}

View file

@ -41,9 +41,10 @@
#include "i_system.h"
#include "i_timer.h"
#include "i_video.h"
#include "z_zone.h"
#include "m_misc.h"
#include "v_video.h"
#include "w_wad.h"
#include "z_zone.h"
#include "r_local.h"
@ -515,12 +516,12 @@ void M_ReadSaveStrings(void)
for (i = 0;i < load_end;i++)
{
strcpy(name, P_SaveGameFile(i));
M_StringCopy(name, P_SaveGameFile(i), sizeof(name));
handle = fopen(name, "rb");
if (handle == NULL)
{
strcpy(&savegamestrings[i][0], EMPTYSTRING);
M_StringCopy(savegamestrings[i], EMPTYSTRING, SAVESTRINGSIZE);
LoadMenu[i].status = 0;
continue;
}
@ -580,7 +581,7 @@ void M_LoadSelect(int choice)
{
char name[256];
strcpy(name, P_SaveGameFile(choice));
M_StringCopy(name, P_SaveGameFile(choice), sizeof(name));
G_LoadGame (name);
M_ClearMenus ();
@ -645,8 +646,8 @@ void M_SaveSelect(int choice)
saveStringEnter = 1;
saveSlot = choice;
strcpy(saveOldString,savegamestrings[choice]);
if (!strcmp(savegamestrings[choice],EMPTYSTRING))
M_StringCopy(saveOldString,savegamestrings[choice], SAVESTRINGSIZE);
if (!strcmp(savegamestrings[choice], EMPTYSTRING))
savegamestrings[choice][0] = 0;
saveCharIndex = strlen(savegamestrings[choice]);
}
@ -1597,7 +1598,8 @@ boolean M_Responder (event_t* ev)
case KEY_ESCAPE:
saveStringEnter = 0;
strcpy(&savegamestrings[saveSlot][0],saveOldString);
M_StringCopy(savegamestrings[saveSlot], saveOldString,
SAVESTRINGSIZE);
break;
case KEY_ENTER:
@ -1987,18 +1989,25 @@ void M_Drawer (void)
int foundnewline = 0;
for (i = 0; i < strlen(messageString + start); i++)
{
if (messageString[start + i] == '\n')
{
memset(string, 0, sizeof(string));
strncpy(string, messageString + start, i);
M_StringCopy(string, messageString + start,
sizeof(string));
if (i < sizeof(string))
{
string[i] = '\0';
}
foundnewline = 1;
start += i + 1;
break;
}
}
if (!foundnewline)
{
strcpy(string, messageString + start);
M_StringCopy(string, messageString + start, sizeof(string));
start += strlen(string);
}

View file

@ -36,6 +36,7 @@
#include "w_wad.h"
#include "doomdef.h"
#include "m_misc.h"
#include "r_local.h"
#include "p_local.h"
@ -494,13 +495,13 @@ void R_InitTextures (void)
name[8] = 0;
names = W_CacheLumpName (DEH_String("PNAMES"), PU_STATIC);
nummappatches = LONG ( *((int *)names) );
name_p = names+4;
name_p = names + 4;
patchlookup = Z_Malloc(nummappatches*sizeof(*patchlookup), PU_STATIC, NULL);
for (i=0 ; i<nummappatches ; i++)
for (i = 0; i < nummappatches; i++)
{
strncpy (name,name_p+i*8, 8);
patchlookup[i] = W_CheckNumForName (name);
M_StringCopy(name, name_p + i * 8, sizeof(name));
patchlookup[i] = W_CheckNumForName(name);
}
W_ReleaseLumpName(DEH_String("PNAMES"));

View file

@ -29,6 +29,7 @@
#include "z_zone.h"
#include "m_misc.h"
#include "m_random.h"
#include "deh_main.h"
@ -1692,17 +1693,15 @@ static void WI_loadUnloadData(load_callback_t callback)
if (gamemode == commercial)
{
strncpy(name, DEH_String("INTERPIC"), 9);
name[8] = '\0';
M_StringCopy(name, DEH_String("INTERPIC"), sizeof(name));
}
else if (gamemode == retail && wbs->epsd == 3)
{
strncpy(name, DEH_String("INTERPIC"), 9);
name[8] = '\0';
M_StringCopy(name, DEH_String("INTERPIC"), sizeof(name));
}
else
{
DEH_snprintf(name, 9, "WIMAP%d", wbs->epsd);
DEH_snprintf(name, sizeof(name), "WIMAP%d", wbs->epsd);
}
// Draw backdrop and save to a temporary buffer