Skip to content

Commit bb94d5b

Browse files
authored
Merge pull request #1196 from asarium/fix/sprintfAttribute
Add format string annotations to all printf style functions
2 parents 8caaf5c + 07107ce commit bb94d5b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+228
-221
lines changed

cmake/toolchain-clang.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ endif()
6464

6565
set(COMPILER_FLAGS "${COMPILER_FLAGS} ${SANITIZE_FLAGS}")
6666

67+
set(COMPILER_FLAGS "${COMPILER_FLAGS} -Wformat-security")
68+
6769
set(COMPILER_FLAGS "${COMPILER_FLAGS} -Wno-unused-function")
6870

6971
# Dear Clang, please tell us if a function does not return a value since that part of the standard is stupid!

cmake/toolchain-gcc.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ endif()
7676

7777
set(COMPILER_FLAGS "${COMPILER_FLAGS} ${SANITIZE_FLAGS}")
7878

79+
set(COMPILER_FLAGS "${COMPILER_FLAGS} -Wformat-security")
80+
7981
set(COMPILER_FLAGS "${COMPILER_FLAGS} -Wno-unused-function")
8082

8183
# Dear GCC, please tell us if a function does not return a value since that part of the standard is stupid!

code/bmpman/bmpman.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ DCF(bmpman, "Shows/changes bitmap caching parameters and usage") {
418418
}
419419

420420
if (dc_optional_string_either("status", "--status") || dc_optional_string_either("?", "--?")) {
421-
dc_printf("Total RAM usage: %d bytes\n", bm_texture_ram);
421+
dc_printf("Total RAM usage: " SIZE_T_ARG " bytes\n", bm_texture_ram);
422422

423423
if (Bm_max_ram > 1024 * 1024) {
424424
dc_printf("\tMax RAM allowed: %.1f MB\n", i2fl(Bm_max_ram) / (1024.0f*1024.0f));
@@ -434,14 +434,14 @@ DCF(bmpman, "Shows/changes bitmap caching parameters and usage") {
434434

435435

436436
if (dc_optional_string("flush")) {
437-
dc_printf("Total RAM usage before flush: %d bytes\n", bm_texture_ram);
437+
dc_printf("Total RAM usage before flush: " SIZE_T_ARG " bytes\n", bm_texture_ram);
438438
int i;
439439
for (i = 0; i < MAX_BITMAPS; i++) {
440440
if (bm_bitmaps[i].type != BM_TYPE_NONE) {
441441
bm_free_data(i);
442442
}
443443
}
444-
dc_printf("Total RAM after flush: %d bytes\n", bm_texture_ram);
444+
dc_printf("Total RAM after flush: " SIZE_T_ARG " bytes\n", bm_texture_ram);
445445
} else if (dc_optional_string("ram")) {
446446
dc_stuff_int(&Bm_max_ram);
447447

code/controlconfig/controlsconfig.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1974,7 +1974,7 @@ void control_config_do_frame(float frametime)
19741974
if (t % 2) {
19751975
gr_set_color_fast(&Color_text_normal);
19761976
gr_get_string_size(&w, NULL, XSTR( "?", 208));
1977-
gr_printf_menu(x - w / 2, y - font_height / 2, XSTR( "?", 208));
1977+
gr_printf_menu(x - w / 2, y - font_height / 2, "%s", XSTR( "?", 208));
19781978
}
19791979

19801980
} else if (!(z & JOY_AXIS) && ((Conflicts[z].key >= 0) || (Conflicts[z].joy >= 0))) {
@@ -1986,7 +1986,7 @@ void control_config_do_frame(float frametime)
19861986
gr_set_color_fast(&Color_text_normal);
19871987
str = XSTR( "Control conflicts with:", 209);
19881988
gr_get_string_size(&w, NULL, str);
1989-
gr_printf_menu(x - w / 2, y - font_height, str);
1989+
gr_printf_menu(x - w / 2, y - font_height, "%s", str);
19901990

19911991
if (Control_config[i].hasXSTR) {
19921992
strcpy_s(buf, XSTR(Control_config[i].text, CONTROL_CONFIG_XSTR + i));
@@ -1996,20 +1996,20 @@ void control_config_do_frame(float frametime)
19961996

19971997
font::force_fit_string(buf, 255, Conflict_wnd_coords[gr_screen.res][CONTROL_W_COORD]);
19981998
gr_get_string_size(&w, NULL, buf);
1999-
gr_printf_menu(x - w / 2, y, buf);
1999+
gr_printf_menu(x - w / 2, y, "%s", buf);
20002000

20012001
} else if (*bound_string) {
20022002
gr_set_color_fast(&Color_text_normal);
20032003
gr_get_string_size(&w, NULL, bound_string);
2004-
gr_printf_menu(x - w / 2, y - font_height / 2, bound_string);
2004+
gr_printf_menu(x - w / 2, y - font_height / 2, "%s", bound_string);
20052005
if (timestamp_elapsed(bound_timestamp)) {
20062006
*bound_string = 0;
20072007
}
20082008
}
20092009

20102010
if (Cc_lines[Num_cc_lines - 1].y + font_height > Cc_lines[Scroll_offset].y + Control_list_coords[gr_screen.res][CONTROL_H_COORD]) {
20112011
gr_set_color_fast(&Color_white);
2012-
gr_printf_menu(Control_more_coords[gr_screen.res][CONTROL_X_COORD], Control_more_coords[gr_screen.res][CONTROL_Y_COORD], XSTR( "More...", 210));
2012+
gr_printf_menu(Control_more_coords[gr_screen.res][CONTROL_X_COORD], Control_more_coords[gr_screen.res][CONTROL_Y_COORD], "%s", XSTR( "More...", 210));
20132013
}
20142014

20152015
conflict = 0;
@@ -2035,7 +2035,7 @@ void control_config_do_frame(float frametime)
20352035
if (Cc_lines[line].label) {
20362036
strcpy_s(buf, Cc_lines[line].label);
20372037
font::force_fit_string(buf, 255, Control_list_ctrl_w[gr_screen.res]);
2038-
gr_printf_menu(Control_list_coords[gr_screen.res][CONTROL_X_COORD], y, buf);
2038+
gr_printf_menu(Control_list_coords[gr_screen.res][CONTROL_X_COORD], y, "%s", buf);
20392039
}
20402040

20412041
if (!(z & JOY_AXIS)) {
@@ -2046,7 +2046,7 @@ void control_config_do_frame(float frametime)
20462046

20472047
if ((k < 0) && (j < 0)) {
20482048
gr_set_color_fast(&Color_grey);
2049-
gr_printf_menu(x, y, XSTR( "None", 211));
2049+
gr_printf_menu(x, y, "%s", XSTR( "None", 211));
20502050

20512051
} else {
20522052
if (k >= 0) {
@@ -2066,7 +2066,7 @@ void control_config_do_frame(float frametime)
20662066
gr_set_color_fast(c);
20672067
}
20682068

2069-
gr_printf_menu(x, y, buf);
2069+
gr_printf_menu(x, y, "%s", buf);
20702070

20712071
Cc_lines[line].kx = x - Control_list_coords[gr_screen.res][CONTROL_X_COORD];
20722072
gr_get_string_size(&w, NULL, buf);
@@ -2075,7 +2075,7 @@ void control_config_do_frame(float frametime)
20752075

20762076
if (j >= 0) {
20772077
gr_set_color_fast(&Color_text_normal);
2078-
gr_printf_menu(x, y, XSTR( ", ", 212));
2078+
gr_printf_menu(x, y, "%s", XSTR( ", ", 212));
20792079
gr_get_string_size(&w, NULL, XSTR( ", ", 212));
20802080
x += w;
20812081
}
@@ -2099,7 +2099,7 @@ void control_config_do_frame(float frametime)
20992099
}
21002100

21012101
font::force_fit_string(buf, 255, Control_list_key_w[gr_screen.res] + Control_list_key_x[gr_screen.res] - x);
2102-
gr_printf_menu(x, y, buf);
2102+
gr_printf_menu(x, y, "%s", buf);
21032103

21042104
Cc_lines[line].jx = x - Control_list_coords[gr_screen.res][CONTROL_X_COORD];
21052105
gr_get_string_size(&Cc_lines[line].jw, NULL, buf);
@@ -2115,7 +2115,7 @@ void control_config_do_frame(float frametime)
21152115

21162116
if (j < 0) {
21172117
gr_set_color_fast(&Color_grey);
2118-
gr_printf_menu(x, y, XSTR( "None", 211));
2118+
gr_printf_menu(x, y, "%s", XSTR( "None", 211));
21192119

21202120
} else {
21212121
if (Conflicts_axes[z & ~JOY_AXIS] >= 0) {

code/cutscene/cutscenes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ void cutscenes_screen_do_frame()
585585
}
586586

587587
gr_printf_menu(Cutscene_list_coords[gr_screen.res][0], Cutscene_list_coords[gr_screen.res][1] + y,
588-
Cutscenes[Cutscene_list[z]].name);
588+
"%s", Cutscenes[Cutscene_list[z]].name);
589589

590590
y += font_height;
591591
z++;

code/debugconsole/console.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ bool dc_pause_output(void);
268268
* @brief Prints the given char string to the debug console
269269
* @details See the doc for std::printf() for formating and more details
270270
*/
271-
void dc_printf(const char *format, ...);
271+
void dc_printf(SCP_FORMAT_STRING const char *format, ...) SCP_FORMAT_STRING_ARGS(1, 2);
272272

273273
/**
274274
* @brief Opens and processes the debug console. (Blocking call)

code/debugconsole/consoleparse.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,7 @@ long dc_parse_long(const char *ch, dc_token type) {
957957
dc_printf("Error: Could not convert all of the buffer '%s'.\n", buffer_str.c_str());
958958
if (Dc_debug_on) {
959959
dc_printf("<debug> Buffer value: %s\n", buffer_str.c_str());
960-
dc_printf("<debug> Return value: %i", ret);
960+
dc_printf("<debug> Return value: %ld", ret);
961961
}
962962
throw errParse(ch, type);
963963
}
@@ -1046,7 +1046,7 @@ ulong dc_parse_ulong(const char *ch, dc_token type) {
10461046
dc_printf("Error: Could not convert all of the buffer '%s'.\n", buffer_str.c_str());
10471047
if (Dc_debug_on) {
10481048
dc_printf("<debug> Buffer value: %s\n", buffer_str.c_str());
1049-
dc_printf("<debug> Return value: %i", ret);
1049+
dc_printf("<debug> Return value: %ld", ret);
10501050
}
10511051
throw errParse(ch, type);
10521052
}

code/fs2netd/fs2netd_client.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "playerman/player.h"
3535
#include "popup/popup.h"
3636
#include "osapi/osapi.h"
37+
#include "hud/hudmessage.h"
3738

3839
#ifndef WIN32
3940
#include <cstdio>
@@ -53,7 +54,6 @@
5354

5455

5556
extern int Multi_debrief_stats_accept_code;
56-
extern void HUD_printf(const char *format, ...);
5757
extern int game_hacked_data();
5858

5959

@@ -141,7 +141,7 @@ void fs2netd_options_config_init()
141141
long port_tmp = strtol(Multi_options_g.tracker_port, (char**)NULL, 10);
142142

143143
if ( (port_tmp < 1024) || (port_tmp > USHRT_MAX) ) {
144-
ml_printf("NOTICE: The port specified for game/user trackers, '%i', is outside of the required range, %i through %i!", port_tmp, 1024, USHRT_MAX);
144+
ml_printf("NOTICE: The port specified for game/user trackers, '%ld', is outside of the required range, %i through %i!", port_tmp, 1024, USHRT_MAX);
145145
ml_printf("NOTICE: Port for game/user trackers is invalid, using default instead (%s).", FS2NETD_DEFAULT_PORT);
146146
strncpy( Multi_options_g.tracker_port, FS2NETD_DEFAULT_PORT, STD_NAME_LEN );
147147
}
@@ -689,7 +689,7 @@ static void fs2netd_handle_messages()
689689
case NETGAME_STATE_IN_MISSION: // gotta make it paused
690690
//multi_pause_request(1);
691691
//send_game_chat_packet(Net_player, str, MULTI_MSG_ALL, NULL);
692-
HUD_printf(tbuf);
692+
HUD_printf("%s", tbuf);
693693
break;
694694

695695
default:

code/fs2netd/tcp_client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ int FS2NetD_ValidateTableList(bool do_send)
768768
}
769769

770770
if ( num_tables > (int)Table_valid_status.size() ) {
771-
ml_printf("FS2NetD WARNING: Table list contains %i tables, but we only requested %i! Invalid data!", num_tables, Table_valid_status.size());
771+
ml_printf("FS2NetD WARNING: Table list contains %i tables, but we only requested " SIZE_T_ARG "! Invalid data!", num_tables, Table_valid_status.size());
772772
return -1;
773773
}
774774

code/gamehelp/contexthelp.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,8 @@ void help_overlay_blit(int overlay_id, int resolution_index)
517517
gr_get_string_size(&width, &height, help_overlaylist[overlay_id].textlist.at(0).at(idx).string, (int)strlen(help_overlaylist[overlay_id].textlist.at(0).at(idx).string));
518518
gr_rect(help_overlaylist[overlay_id].textlist.at(resolution_index).at(idx).x_coord-2*HELP_PADDING, help_overlaylist[overlay_id].textlist.at(resolution_index).at(idx).y_coord-3*HELP_PADDING, width+4*HELP_PADDING, height+4*HELP_PADDING, GR_RESIZE_MENU);
519519
gr_set_color_fast(&Color_bright_white);
520-
gr_printf_menu(help_overlaylist[overlay_id].textlist.at(resolution_index).at(idx).x_coord, help_overlaylist[overlay_id].textlist.at(resolution_index).at(idx).y_coord, help_overlaylist[overlay_id].textlist.at(0).at(idx).string);
520+
gr_printf_menu(help_overlaylist[overlay_id].textlist.at(resolution_index).at(idx).x_coord, help_overlaylist[overlay_id].textlist.at(resolution_index).at(idx).y_coord,
521+
"%s", help_overlaylist[overlay_id].textlist.at(0).at(idx).string);
521522
}
522523
font::set_font(font::FONT1);
523524

@@ -606,7 +607,7 @@ void showlbracketpos(int num)
606607
void showplinepos(int plinenum)
607608
{
608609
int i;
609-
dc_printf("pline %d on overlay %d vertices are now ", plinenum, current_helpid, help_overlaylist[current_helpid].textlist.at(current_resolution).at(plinenum).y_coord );
610+
dc_printf("pline %d on overlay %d vertices are now ", plinenum, current_helpid );
610611
for (i=0; i<help_overlaylist[current_helpid].plinelist.at(gr_screen.res).at(plinenum).vtxcount; i++)
611612
{
612613
dc_printf("(%3.0f %3.0f) ", help_overlaylist[current_helpid].plinelist.at(current_resolution).at(plinenum).vtx.at(i).xyz.x, help_overlaylist[current_helpid].plinelist.at(current_resolution).at(plinenum).vtx.at(i).xyz.y);

0 commit comments

Comments
 (0)