Skip to content

Commit 5f8e2c9

Browse files
committed
fix the size parameter of snprintf and vsnprintf
These functions accept a size parameter that *includes* the null terminator, so tweak function calls accordingly.
1 parent 2ceb523 commit 5f8e2c9

File tree

12 files changed

+41
-41
lines changed

12 files changed

+41
-41
lines changed

code/cfile/cfilesystem.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2480,7 +2480,7 @@ void cfile_spew_pack_file_crcs()
24802480
my_time = time(NULL);
24812481

24822482
memset( datetime, 0, sizeof(datetime) );
2483-
snprintf(datetime, sizeof(datetime)-1, "%s", ctime(&my_time));
2483+
snprintf(datetime, sizeof(datetime), "%s", ctime(&my_time));
24842484
// ctime() adds a newline char, so we have to strip it off
24852485
datetime[strlen(datetime)-1] = '\0';
24862486

code/fireball/fireballs.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,45 +107,45 @@ void fireball_play_warphole_close_sound(fireball *fb)
107107
snd_play_3d(gamesnd_get_game_sound(sound_index), &fireball_objp->pos, &Eye_position, fireball_objp->radius, NULL, 0, 1.0F, SND_PRIORITY_SINGLE_INSTANCE, NULL, fb->warp_sound_range_multiplier); // play warp sound effect
108108
}
109109

110-
static void fireball_generate_unique_id(char *unique_id, int buffer_len, int fireball_index)
110+
static void fireball_generate_unique_id(char *unique_id, size_t buffer_size, int fireball_index)
111111
{
112112
Assertion(SCP_vector_inbounds(Fireball_info, fireball_index), "fireball_index is out of bounds!");
113113

114114
switch (fireball_index)
115115
{
116116
// use sensible names for the fireball.tbl default entries
117117
case FIREBALL_EXPLOSION_MEDIUM:
118-
strncpy(unique_id, "Medium Explosion", buffer_len);
118+
strncpy(unique_id, "Medium Explosion", buffer_size-1);
119119
break;
120120

121121
case FIREBALL_WARP:
122-
strncpy(unique_id, "Warp Effect", buffer_len);
122+
strncpy(unique_id, "Warp Effect", buffer_size-1);
123123
break;
124124

125125
case FIREBALL_KNOSSOS:
126-
strncpy(unique_id, "Knossos Effect", buffer_len);
126+
strncpy(unique_id, "Knossos Effect", buffer_size-1);
127127
break;
128128

129129
case FIREBALL_ASTEROID:
130-
strncpy(unique_id, "Asteroid Explosion", buffer_len);
130+
strncpy(unique_id, "Asteroid Explosion", buffer_size-1);
131131
break;
132132

133133
case FIREBALL_EXPLOSION_LARGE1:
134-
strncpy(unique_id, "Large Explosion 1", buffer_len);
134+
strncpy(unique_id, "Large Explosion 1", buffer_size-1);
135135
break;
136136

137137
case FIREBALL_EXPLOSION_LARGE2:
138-
strncpy(unique_id, "Large Explosion 2", buffer_len);
138+
strncpy(unique_id, "Large Explosion 2", buffer_size-1);
139139
break;
140140

141141
// base the id on the index
142142
default:
143-
snprintf(unique_id, buffer_len, "Custom Fireball %d", fireball_index - NUM_DEFAULT_FIREBALLS + 1);
143+
snprintf(unique_id, buffer_size, "Custom Fireball %d", fireball_index - NUM_DEFAULT_FIREBALLS + 1);
144144
break;
145145
}
146146

147147
// null-terminate
148-
unique_id[buffer_len - 1] = '\0';
148+
unique_id[buffer_size-1] = '\0';
149149
}
150150

151151
/**

code/graphics/opengl/gropengl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ void gr_opengl_print_screen(const char *filename)
276276
GLuint pbo = 0;
277277

278278
// save to a "screenshots" directory and tack on the filename
279-
snprintf(tmp, MAX_PATH_LEN-1, "screenshots/%s.png", filename);
279+
snprintf(tmp, MAX_PATH_LEN, "screenshots/%s.png", filename);
280280

281281
_mkdir(os_get_config_path("screenshots").c_str());
282282

@@ -436,7 +436,7 @@ void gr_opengl_dump_envmap(const char* filename)
436436
glBindTexture(sphere_tex->texture_target, sphere_tex->texture_id);
437437
pixels = (GLubyte*)vm_malloc(sphere_width * sphere_height * 4, memory::quiet_alloc);
438438
glGetTexImage(sphere_tex->texture_target, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
439-
snprintf(tmp, MAX_PATH_LEN - 1, "envmaps/%s.png", filename);
439+
snprintf(tmp, MAX_PATH_LEN, "envmaps/%s.png", filename);
440440
if (!png_write_bitmap(os_get_config_path(tmp).c_str(), 4 * width, 2 * height, true, pixels)) {
441441
ReleaseWarning(LOCATION, "Failed to write envmap to \"%s\".", os_get_config_path(tmp).c_str());
442442
}

code/graphics/software/font.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ void gr_printf(int x, int y, const char * format, ...)
777777
if (!FontManager::isReady()) return;
778778

779779
va_start(args, format);
780-
vsnprintf(grx_printf_text, sizeof(grx_printf_text) - 1, format, args);
780+
vsnprintf(grx_printf_text, sizeof(grx_printf_text), format, args);
781781
va_end(args);
782782
grx_printf_text[sizeof(grx_printf_text) - 1] = '\0';
783783

@@ -791,7 +791,7 @@ void gr_printf_menu(int x, int y, const char * format, ...)
791791
if (!FontManager::isReady()) return;
792792

793793
va_start(args, format);
794-
vsnprintf(grx_printf_text, sizeof(grx_printf_text) - 1, format, args);
794+
vsnprintf(grx_printf_text, sizeof(grx_printf_text), format, args);
795795
va_end(args);
796796
grx_printf_text[sizeof(grx_printf_text) - 1] = '\0';
797797

@@ -805,7 +805,7 @@ void gr_printf_menu_zoomed(int x, int y, const char * format, ...)
805805
if (!FontManager::isReady()) return;
806806

807807
va_start(args, format);
808-
vsnprintf(grx_printf_text, sizeof(grx_printf_text) - 1, format, args);
808+
vsnprintf(grx_printf_text, sizeof(grx_printf_text), format, args);
809809
va_end(args);
810810
grx_printf_text[sizeof(grx_printf_text) - 1] = '\0';
811811

@@ -819,7 +819,7 @@ void gr_printf_no_resize(int x, int y, const char * format, ...)
819819
if (!FontManager::isReady()) return;
820820

821821
va_start(args, format);
822-
vsnprintf(grx_printf_text, sizeof(grx_printf_text) - 1, format, args);
822+
vsnprintf(grx_printf_text, sizeof(grx_printf_text), format, args);
823823
va_end(args);
824824
grx_printf_text[sizeof(grx_printf_text) - 1] = '\0';
825825

code/hud/hud.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,7 @@ void HudGauge::renderPrintf(int x, int y, float scale, bool config, const char*
992992

993993
// format the text
994994
va_start(args, format);
995-
vsnprintf(tmp, sizeof(tmp)-1, format, args);
995+
vsnprintf(tmp, sizeof(tmp), format, args);
996996
va_end(args);
997997
tmp[sizeof(tmp)-1] = '\0';
998998

@@ -1006,7 +1006,7 @@ void HudGauge::renderPrintfWithGauge(int x, int y, int gauge_id, float scale, bo
10061006

10071007
// format the text
10081008
va_start(args, format);
1009-
vsnprintf(tmp, sizeof(tmp)-1, format, args);
1009+
vsnprintf(tmp, sizeof(tmp), format, args);
10101010
va_end(args);
10111011
tmp[sizeof(tmp)-1] = '\0';
10121012

code/hud/hudmessage.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ void HUD_fixed_printf(float duration, color col, const char *format, ...)
497497
}
498498

499499
va_start(args, format);
500-
vsnprintf(tmp, sizeof(tmp)-1, format, args);
500+
vsnprintf(tmp, sizeof(tmp), format, args);
501501
va_end(args);
502502
tmp[sizeof(tmp)-1] = '\0';
503503

@@ -553,7 +553,7 @@ void HUD_printf(const char *format, ...)
553553
}
554554

555555
va_start(args, format);
556-
vsnprintf(tmp, sizeof(tmp)-1, format, args);
556+
vsnprintf(tmp, sizeof(tmp), format, args);
557557
va_end(args);
558558
tmp[sizeof(tmp)-1] = '\0';
559559

@@ -579,7 +579,7 @@ void HUD_sourced_printf(int source, const char *format, ...)
579579
}
580580

581581
va_start(args, format);
582-
vsnprintf(tmp, sizeof(tmp)-1, format, args);
582+
vsnprintf(tmp, sizeof(tmp), format, args);
583583
va_end(args);
584584
tmp[sizeof(tmp)-1] = '\0';
585585

code/lab/dialogs/lab_ui_helpers.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ SCP_string get_ship_table_text(ship_info* sip)
143143
if (!stricmp(line2 + i, sip->name)) {
144144
memset(file_text, 0, sizeof(file_text));
145145
snprintf(file_text,
146-
sizeof(file_text) - 1,
146+
sizeof(file_text),
147147
"-- %s -------------------------------\r\n",
148148
tbl_file_names[n].c_str());
149149
result += file_text;
@@ -269,7 +269,7 @@ SCP_string get_weapon_table_text(weapon_info* wip)
269269
if (!stricmp(line2 + i, wip->name)) {
270270
memset(file_text, 0, sizeof(file_text));
271271
snprintf(file_text,
272-
sizeof(file_text) - 1,
272+
sizeof(file_text),
273273
"-- %s -------------------------------\r\n",
274274
tbl_file_names[n].c_str());
275275
result += file_text;
@@ -391,7 +391,7 @@ SCP_string get_asteroid_table_text(const asteroid_info* aip)
391391
if (!stricmp(line2 + i, aip->name)) {
392392
memset(file_text, 0, sizeof(file_text));
393393
snprintf(file_text,
394-
sizeof(file_text) - 1,
394+
sizeof(file_text),
395395
"-- %s -------------------------------\r\n",
396396
tbl_file_names[n].c_str());
397397
result += file_text;

code/network/multiui.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4457,7 +4457,7 @@ void multi_create_list_load_missions()
44574457
Multi_create_mission_list.clear();
44584458

44594459
memset( wild_card, 0, sizeof(wild_card) );
4460-
snprintf(wild_card, sizeof(wild_card) - 1, "*%s", FS_MISSION_FILE_EXT);
4460+
snprintf(wild_card, sizeof(wild_card), "*%s", FS_MISSION_FILE_EXT);
44614461

44624462
file_list = (char**) vm_malloc( sizeof(char*) * 1024 );
44634463

@@ -4550,7 +4550,7 @@ void multi_create_list_load_campaigns()
45504550
Multi_create_campaign_list.clear();
45514551

45524552
memset( wild_card, 0, sizeof(wild_card) );
4553-
snprintf(wild_card, sizeof(wild_card) - 1, "*%s", FS_CAMPAIGN_FILE_EXT);
4553+
snprintf(wild_card, sizeof(wild_card), "*%s", FS_CAMPAIGN_FILE_EXT);
45544554

45554555
file_list = (char**) vm_malloc( sizeof(char*) * 1024 );
45564556

code/network/stand_gui.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,7 +1052,7 @@ static HWND Player_stats[MAX_PLAYER_STAT_FIELDS]; // text boxes for player allt
10521052
static HWND Player_mstats[MAX_PLAYER_STAT_FIELDS]; // text boxes for player mission statistics info
10531053

10541054
// sprintf and set window text to the passed int
1055-
#define STD_ADDSTRING(hwnd,val) { snprintf(txt,sizeof(txt)-1,"%d",(int)val); SetWindowText(hwnd,txt); }
1055+
#define STD_ADDSTRING(hwnd,val) { snprintf(txt,sizeof(txt),"%d",sz2i(val)); SetWindowText(hwnd,txt); }
10561056

10571057
// intialize all the controls in the player info tab
10581058
void std_pinfo_init_player_info_controls();
@@ -2036,17 +2036,17 @@ void std_build_title_string(char *str)
20362036
memset(ver_str, 0, sizeof(ver_str));
20372037

20382038
if (FS_VERSION_BUILD == 0 && FS_VERSION_HAS_REVIS == 0) { //-V547
2039-
snprintf(ver_str, sizeof(ver_str)-1, "%i.%i", FS_VERSION_MAJOR, FS_VERSION_MINOR);
2039+
snprintf(ver_str, sizeof(ver_str), "%i.%i", FS_VERSION_MAJOR, FS_VERSION_MINOR);
20402040
} else if (FS_VERSION_HAS_REVIS == 0) {
2041-
snprintf(ver_str, sizeof(ver_str)-1, "%i.%i.%i", FS_VERSION_MAJOR, FS_VERSION_MINOR, FS_VERSION_BUILD);
2041+
snprintf(ver_str, sizeof(ver_str), "%i.%i.%i", FS_VERSION_MAJOR, FS_VERSION_MINOR, FS_VERSION_BUILD);
20422042
} else {
2043-
snprintf(ver_str, sizeof(ver_str)-1, "%i.%i.%i.%i", FS_VERSION_MAJOR, FS_VERSION_MINOR, FS_VERSION_BUILD, FS_VERSION_REVIS);
2043+
snprintf(ver_str, sizeof(ver_str), "%i.%i.%i.%i", FS_VERSION_MAJOR, FS_VERSION_MINOR, FS_VERSION_BUILD, FS_VERSION_REVIS);
20442044
}
20452045

20462046
// now build the title
20472047
memset(temp, 0, 256);
20482048

2049-
snprintf(temp, sizeof(temp)-1, "%s %s", XSTR("FreeSpace Standalone", 935), ver_str);
2049+
snprintf(temp, sizeof(temp), "%s %s", XSTR("FreeSpace Standalone", 935), ver_str);
20502050

20512051
// output first part
20522052
strcpy(str, temp);
@@ -2105,22 +2105,22 @@ static HMENU std_create_systray_menu()
21052105
HMENU stdPopup = CreatePopupMenu();
21062106

21072107
// Type of connection:
2108-
snprintf(tstr, sizeof(tstr)-1, "Connection Type: %s", MULTI_IS_TRACKER_GAME ? "PXO" : "Local/IP");
2108+
snprintf(tstr, sizeof(tstr), "Connection Type: %s", MULTI_IS_TRACKER_GAME ? "PXO" : "Local/IP");
21092109
AppendMenu(stdPopup, MF_STRING | MF_GRAYED, 0, tstr);
21102110

21112111
// ----------------------------------------------
21122112
AppendMenu(stdPopup, MF_SEPARATOR, 0, NULL);
21132113

21142114
// Game name:
2115-
snprintf(tstr, sizeof(tstr)-1, "Name: %s", Netgame.name);
2115+
snprintf(tstr, sizeof(tstr), "Name: %s", Netgame.name);
21162116
AppendMenu(stdPopup, MF_STRING | MF_GRAYED, 0, tstr);
21172117

21182118
// Mission name:
2119-
snprintf(tstr, sizeof(tstr)-1, "Mission: %s", strlen(Netgame.mission_name) ? Netgame.mission_name : "<none>");
2119+
snprintf(tstr, sizeof(tstr), "Mission: %s", strlen(Netgame.mission_name) ? Netgame.mission_name : "<none>");
21202120
AppendMenu(stdPopup, MF_STRING | MF_GRAYED, 0, tstr);
21212121

21222122
// Number of players:
2123-
snprintf(tstr, sizeof(tstr)-1, "Num Players: %d", multi_num_players());
2123+
snprintf(tstr, sizeof(tstr), "Num Players: %d", multi_num_players());
21242124
AppendMenu(stdPopup, MF_STRING | MF_GRAYED, 0, tstr);
21252125

21262126
// ----------------------------------------------

code/osapi/osregistry.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1108,7 +1108,7 @@ void os_config_write_uint(const char* section, const char* name, unsigned int va
11081108

11091109
char buf[21];
11101110

1111-
snprintf(buf, 20, "%u", value);
1111+
snprintf(buf, 21, "%u", value);
11121112

11131113
profile = profile_update(profile, section, name, buf);
11141114
profile_save(profile, file);

0 commit comments

Comments
 (0)