Skip to content

Commit d8aeaa9

Browse files
committed
Mark the *printf functions with __attribute__((format)).
Using __attribute__((format)) means that compilers supporting the attribute (GCC, clang, icc, perhaps others) will automatically issue warnings when printf arguments are incorrectly typed (i.e. inconsistent with the type specified in the format string).
1 parent d3b9846 commit d8aeaa9

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

printf.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ void _putchar(char character);
5858
* \return The number of characters that are written into the array, not counting the terminating null character
5959
*/
6060
#define printf printf_
61+
#ifdef __GNUC__
62+
__attribute__ ((format (__printf__, 1, 2)))
63+
#endif
6164
int printf_(const char* format, ...);
6265

6366

@@ -69,6 +72,9 @@ int printf_(const char* format, ...);
6972
* \return The number of characters that are WRITTEN into the buffer, not counting the terminating null character
7073
*/
7174
#define sprintf sprintf_
75+
#ifdef __GNUC__
76+
__attribute__ ((format (__printf__, 2, 3)))
77+
#endif
7278
int sprintf_(char* buffer, const char* format, ...);
7379

7480

@@ -84,7 +90,14 @@ int sprintf_(char* buffer, const char* format, ...);
8490
*/
8591
#define snprintf snprintf_
8692
#define vsnprintf vsnprintf_
93+
#ifdef __GNUC__
94+
__attribute__ ((format (__printf__, 3, 4)))
95+
#endif
8796
int snprintf_(char* buffer, size_t count, const char* format, ...);
97+
98+
#ifdef __GNUC__
99+
__attribute__ ((format (__printf__, 3, 0)))
100+
#endif
88101
int vsnprintf_(char* buffer, size_t count, const char* format, va_list va);
89102

90103

@@ -95,6 +108,9 @@ int vsnprintf_(char* buffer, size_t count, const char* format, va_list va);
95108
* \return The number of characters that are WRITTEN into the buffer, not counting the terminating null character
96109
*/
97110
#define vprintf vprintf_
111+
#ifdef __GNUC__
112+
__attribute__ ((format (__printf__, 1, 0)))
113+
#endif
98114
int vprintf_(const char* format, va_list va);
99115

100116

@@ -106,6 +122,9 @@ int vprintf_(const char* format, va_list va);
106122
* \param format A string that specifies the format of the output
107123
* \return The number of characters that are sent to the output function, not counting the terminating null character
108124
*/
125+
#ifdef __GNUC__
126+
__attribute__ ((format (__printf__, 3, 4)))
127+
#endif
109128
int fctprintf(void (*out)(char character, void* arg), void* arg, const char* format, ...);
110129

111130

0 commit comments

Comments
 (0)