From 8eb3200286d523379295143ce3f44d77ce036d4b Mon Sep 17 00:00:00 2001 From: Simon Howard Date: Tue, 1 Apr 2014 20:43:45 -0400 Subject: Replace all snprintf() calls with M_snprintf(). The Windows API has an _snprintf function that is not the same as Unix's snprintf(): if the string is truncated then no trailing NUL character is appended. This makes the function unsafe. Define a replacement/wrapper called M_snprintf that works the same but always appends a trailing NUL, for safety on Windows and other OSes that behave like this. Do the same thing for vsnprintf(), and update HACKING to list snprintf/vsnprintf as forbidden functions. This fixes #375; thanks to Quasar for pointing out the different behavior of these functions. --- src/hexen/sb_bar.c | 44 ++++++++++++++++++++++---------------------- 1 file changed, 22 insertions(+), 22 deletions(-) (limited to 'src/hexen/sb_bar.c') diff --git a/src/hexen/sb_bar.c b/src/hexen/sb_bar.c index b35c1f51..5d2438aa 100644 --- a/src/hexen/sb_bar.c +++ b/src/hexen/sb_bar.c @@ -640,20 +640,20 @@ static void DrawSoundInfo(void) MN_DrTextA("------", xPos[0], y); continue; } - snprintf(text, sizeof(text), "%s", c->name); + M_snprintf(text, sizeof(text), "%s", c->name); M_ForceUppercase(text); MN_DrTextA(text, xPos[x++], y); - snprintf(text, sizeof(text), "%d", c->mo->type); + M_snprintf(text, sizeof(text), "%d", c->mo->type); MN_DrTextA(text, xPos[x++], y); - snprintf(text, sizeof(text), "%d", c->mo->x >> FRACBITS); + M_snprintf(text, sizeof(text), "%d", c->mo->x >> FRACBITS); MN_DrTextA(text, xPos[x++], y); - snprintf(text, sizeof(text), "%d", c->mo->y >> FRACBITS); + M_snprintf(text, sizeof(text), "%d", c->mo->y >> FRACBITS); MN_DrTextA(text, xPos[x++], y); - snprintf(text, sizeof(text), "%d", (int) c->id); + M_snprintf(text, sizeof(text), "%d", (int) c->id); MN_DrTextA(text, xPos[x++], y); - snprintf(text, sizeof(text), "%d", c->priority); + M_snprintf(text, sizeof(text), "%d", c->priority); MN_DrTextA(text, xPos[x++], y); - snprintf(text, sizeof(text), "%d", c->distance); + M_snprintf(text, sizeof(text), "%d", c->distance); MN_DrTextA(text, xPos[x++], y); } UpdateState |= I_FULLSCRN; @@ -1738,7 +1738,7 @@ static void CheatWarpFunc(player_t * player, Cheat_t * cheat) P_SetMessage(player, TXT_CHEATBADINPUT, true); return; } - snprintf(mapName, sizeof(mapName), "MAP%02d", map); + M_snprintf(mapName, sizeof(mapName), "MAP%02d", map); if (W_CheckNumForName(mapName) == -1) { // Can't find P_SetMessage(player, TXT_CHEATNOMAP, true); @@ -1769,7 +1769,7 @@ static void CheatMassacreFunc(player_t * player, Cheat_t * cheat) char buffer[80]; count = P_Massacre(); - snprintf(buffer, sizeof(buffer), "%d MONSTERS KILLED\n", count); + M_snprintf(buffer, sizeof(buffer), "%d MONSTERS KILLED\n", count); P_SetMessage(player, buffer, true); } @@ -1857,12 +1857,12 @@ static void CheatVersionFunc(player_t * player, Cheat_t * cheat) static void CheatDebugFunc(player_t * player, Cheat_t * cheat) { char textBuffer[50]; - snprintf(textBuffer, sizeof(textBuffer), - "MAP %d (%d) X:%5d Y:%5d Z:%5d", - P_GetMapWarpTrans(gamemap), - gamemap, - player->mo->x >> FRACBITS, - player->mo->y >> FRACBITS, player->mo->z >> FRACBITS); + M_snprintf(textBuffer, sizeof(textBuffer), + "MAP %d (%d) X:%5d Y:%5d Z:%5d", + P_GetMapWarpTrans(gamemap), + gamemap, + player->mo->x >> FRACBITS, + player->mo->y >> FRACBITS, player->mo->z >> FRACBITS); P_SetMessage(player, textBuffer, true); } @@ -1897,8 +1897,8 @@ static void CheatScriptFunc3(player_t * player, Cheat_t * cheat) if (P_StartACS(script, 0, script_args, player->mo, NULL, 0)) { - snprintf(textBuffer, sizeof(textBuffer), - "RUNNING SCRIPT %.2d", script); + M_snprintf(textBuffer, sizeof(textBuffer), + "RUNNING SCRIPT %.2d", script); P_SetMessage(player, textBuffer, true); } } @@ -1930,8 +1930,8 @@ static void CheatTrackFunc1(player_t * player, Cheat_t * cheat) P_SetMessage(player, "ERROR INITIALIZING CD", true); } - snprintf(buffer, sizeof(buffer), "ENTER DESIRED CD TRACK (%.2d - %.2d):\n", - I_CDMusFirstTrack(), I_CDMusLastTrack()); + M_snprintf(buffer, sizeof(buffer), "ENTER DESIRED CD TRACK (%.2d - %.2d):\n", + I_CDMusFirstTrack(), I_CDMusLastTrack()); P_SetMessage(player, buffer, true); } @@ -1968,14 +1968,14 @@ static void CheatTrackFunc2(player_t * player, Cheat_t * cheat) if (!S_StartCustomCDTrack(track)) { - snprintf(buffer, sizeof(buffer), - "ERROR WHILE TRYING TO PLAY CD TRACK: %.2d\n", track); + M_snprintf(buffer, sizeof(buffer), + "ERROR WHILE TRYING TO PLAY CD TRACK: %.2d\n", track); P_SetMessage(player, buffer, true); } else { // No error encountered while attempting to play the track - snprintf(buffer, sizeof(buffer), "PLAYING TRACK: %.2d\n", track); + M_snprintf(buffer, sizeof(buffer), "PLAYING TRACK: %.2d\n", track); P_SetMessage(player, buffer, true); } } -- cgit v1.2.3