Skip to content

Commit 2a47955

Browse files
committed
Add format string annotations to all printf style functions
This should help to catch most if not all printf mistakes since those can then be caught by our CI.
1 parent caaaf32 commit 2a47955

File tree

9 files changed

+20
-18
lines changed

9 files changed

+20
-18
lines changed

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/fs2netd/fs2netd_client.cpp

Lines changed: 1 addition & 1 deletion
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

code/graphics/2d.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -876,13 +876,13 @@ bool gr_resize_screen_posf(float *x, float *y, float *w = NULL, float *h = NULL,
876876
// Does formatted printing. This calls gr_string after formatting,
877877
// so if you don't need to format the string, then call gr_string
878878
// directly.
879-
extern void gr_printf( int x, int y, const char * format, ... );
879+
extern void gr_printf( int x, int y, const char * format, SCP_FORMAT_STRING ... ) SCP_FORMAT_STRING_ARGS(3, 4);
880880
// same as gr_printf but positions text correctly in menus
881-
extern void gr_printf_menu( int x, int y, const char * format, ... );
881+
extern void gr_printf_menu( int x, int y, const char * format, SCP_FORMAT_STRING ... ) SCP_FORMAT_STRING_ARGS(3, 4);
882882
// same as gr_printf_menu but accounts for menu zooming
883-
extern void gr_printf_menu_zoomed( int x, int y, const char * format, ... );
883+
extern void gr_printf_menu_zoomed( int x, int y, const char * format, SCP_FORMAT_STRING ... ) SCP_FORMAT_STRING_ARGS(3, 4);
884884
// same as gr_printf but doesn't resize for non-standard resolutions
885-
extern void gr_printf_no_resize( int x, int y, const char * format, ... );
885+
extern void gr_printf_no_resize( int x, int y, const char * format, SCP_FORMAT_STRING ... ) SCP_FORMAT_STRING_ARGS(3, 4);
886886

887887
// Returns the size of the string in pixels in w and h
888888
extern void gr_get_string_size( int *w, int *h, const char * text, int len = 9999 );

code/hud/hud.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,8 @@ class HudGauge
311311
void renderString(int x, int y, const char *str);
312312
void renderString(int x, int y, int gauge_id, const char *str);
313313
void renderStringAlignCenter(int x, int y, int area_width, const char *s);
314-
void renderPrintf(int x, int y, const char* format, ...);
315-
void renderPrintf(int x, int y, int gauge_id, const char* format, ...);
314+
void renderPrintf(int x, int y, SCP_FORMAT_STRING const char* format, ...) SCP_FORMAT_STRING_ARGS(4, 5);
315+
void renderPrintf(int x, int y, int gauge_id, SCP_FORMAT_STRING const char* format, ...) SCP_FORMAT_STRING_ARGS(5, 6);
316316
void renderLine(int x1, int y1, int x2, int y2);
317317
void renderGradientLine(int x1, int y1, int x2, int y2);
318318
void renderRect(int x, int y, int w, int h);

code/hud/hudmessage.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ void hud_init_msg_window();
6565
void hud_clear_msg_buffer();
6666
int HUD_team_get_source(int team);
6767
int HUD_source_get_team(int team);
68-
void HUD_printf(const char *format, ...);
68+
void HUD_printf(SCP_FORMAT_STRING const char *format, ...) SCP_FORMAT_STRING_ARGS(1, 2);
6969
void hud_sourced_print(int source, const char *msg);
70-
void HUD_sourced_printf(int source, const char *format, ...); // send hud message from specified source
71-
void HUD_ship_sent_printf(int sh, const char *format, ...); // send hud message from a specific ship
72-
void HUD_fixed_printf(float duration, color col, const char *format, ...); // Display a single message for duration seconds.
70+
void HUD_sourced_printf(int source, SCP_FORMAT_STRING const char *format, ...) SCP_FORMAT_STRING_ARGS(2, 3); // send hud message from specified source
71+
void HUD_ship_sent_printf(int sh, SCP_FORMAT_STRING const char *format, ...) SCP_FORMAT_STRING_ARGS(2, 3); // send hud message from a specific ship
72+
void HUD_fixed_printf(float duration, color col, SCP_FORMAT_STRING const char *format, ...) SCP_FORMAT_STRING_ARGS(3, 4); // Display a single message for duration seconds.
7373
void HUD_init_fixed_text(); // Clear all pending fixed text.
7474

7575
void HUD_add_to_scrollback(const char *text, int source);

code/network/multi_log.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#ifndef _FREESPACE_MULTIPLAYER_LOGFILE_HEADER_FILE
1313
#define _FREESPACE_MULTIPLAYER_LOGFILE_HEADER_FILE
1414

15+
#include "globalincs/pstypes.h"
16+
1517
// ----------------------------------------------------------------------------------------------------
1618
// MULTI LOGFILE DEFINES/VARS
1719
//
@@ -31,7 +33,7 @@ void multi_log_close();
3133
void multi_log_process();
3234

3335
// printf function itself called by the ml_printf macro
34-
void ml_printf(const char *format, ...);
36+
void ml_printf(SCP_FORMAT_STRING const char *format, ...) SCP_FORMAT_STRING_ARGS(1, 2);
3537

3638
// string print function
3739
void ml_string(const char *string, int add_time = 1);

code/parse/generic_log.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ bool logfile_init(int logfile_type);
3030
void logfile_close(int logfile_type);
3131

3232
// printf function itself called by the log_printf macro
33-
void log_printf(int logfile_type, const char *format, ...);
33+
void log_printf(int logfile_type, SCP_FORMAT_STRING const char *format, ...) SCP_FORMAT_STRING_ARGS(2, 3);
3434

3535
// string print function
3636
void log_string(int logfile_type, const char *string, int add_time = 0);

code/parse/parselo.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ extern void ignore_gray_space();
9191
// error
9292
extern int get_line_num();
9393
extern char *next_tokens();
94-
extern void diag_printf(const char *format, ...);
95-
extern void error_display(int error_level, const char *format, ...);
94+
extern void diag_printf(SCP_FORMAT_STRING const char *format, ...) SCP_FORMAT_STRING_ARGS(1, 2);
95+
extern void error_display(int error_level, SCP_FORMAT_STRING const char *format, ...) SCP_FORMAT_STRING_ARGS(2, 3);
9696

9797
// skip
9898
extern int skip_to_string(const char *pstr, const char *end = NULL);
@@ -289,7 +289,7 @@ extern bool can_construe_as_integer(const char *text);
289289

290290
// Goober5000 (ditto for C++)
291291
extern void vsprintf(SCP_string &dest, const char *format, va_list ap);
292-
extern void sprintf(SCP_string &dest, const char *format, ...);
292+
extern void sprintf(SCP_string &dest, SCP_FORMAT_STRING const char *format, ...) SCP_FORMAT_STRING_ARGS(2, 3);
293293

294294
// Goober5000
295295
extern int subsystem_stricmp(const char *str1, const char *str2);

code/weapon/emp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ int emp_should_blit_gauge();
105105
void emp_hud_string(int x, int y, int gauge_id, const char *str, int resize_mode);
106106

107107
// emp hud printf
108-
void emp_hud_printf(int x, int y, int gauge_id, const char *format, ...);
108+
void emp_hud_printf(int x, int y, int gauge_id, SCP_FORMAT_STRING const char *format, ...) SCP_FORMAT_STRING_ARGS(4, 5);
109109

110110
// throw some jitter into HUD x and y coords
111111
void emp_hud_jitter(int *x, int *y);

0 commit comments

Comments
 (0)