400-Add-format-attribute-to-some-function-declarations.patch 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. From e6683d001a95d7c3d4d992496f00f77e01fcd268 Mon Sep 17 00:00:00 2001
  2. From: Hauke Mehrtens <hauke@hauke-m.de>
  3. Date: Sun, 22 Nov 2015 15:04:23 +0100
  4. Subject: [PATCH v2] Add format attribute to some function declarations
  5. GCC and Clang are able to check the format arguments given to a
  6. function and warn the user if there is a error in the format arguments
  7. or if there is a potential uncontrolled format string security problem
  8. in the code. GCC does this automatically for some functions like
  9. printf(), but it is also possible to annotate other functions in a way
  10. that it will check them too. This feature is used by glibc for many
  11. functions. This patch adds the attribute to the some functions of musl
  12. expect for these functions where gcc automatically adds it.
  13. GCC automatically adds checks for these functions: printf, fprintf,
  14. sprintf, scanf, fscanf, sscanf, strftime, vprintf, vfprintf and
  15. vsprintf.
  16. The documentation from gcc is here:
  17. https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html
  18. The documentation from Clang is here:
  19. http://clang.llvm.org/docs/AttributeReference.html#format-gnu-format
  20. Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
  21. ---
  22. include/err.h | 26 +++++++++++++++++---------
  23. include/monetary.h | 12 ++++++++++--
  24. include/stdio.h | 29 ++++++++++++++++++++---------
  25. include/syslog.h | 12 ++++++++++--
  26. 4 files changed, 57 insertions(+), 22 deletions(-)
  27. --- a/include/err.h
  28. +++ b/include/err.h
  29. @@ -8,15 +8,23 @@
  30. extern "C" {
  31. #endif
  32. -void warn(const char *, ...);
  33. -void vwarn(const char *, va_list);
  34. -void warnx(const char *, ...);
  35. -void vwarnx(const char *, va_list);
  36. +#if __GNUC__ >= 3
  37. +#define __fp(x, y) __attribute__ ((__format__ (__printf__, x, y)))
  38. +#else
  39. +#define __fp(x, y)
  40. +#endif
  41. -_Noreturn void err(int, const char *, ...);
  42. -_Noreturn void verr(int, const char *, va_list);
  43. -_Noreturn void errx(int, const char *, ...);
  44. -_Noreturn void verrx(int, const char *, va_list);
  45. +void warn(const char *, ...) __fp(1, 2);
  46. +void vwarn(const char *, va_list) __fp(1, 0);
  47. +void warnx(const char *, ...) __fp(1, 2);
  48. +void vwarnx(const char *, va_list) __fp(1, 0);
  49. +
  50. +_Noreturn void err(int, const char *, ...) __fp(2, 3);
  51. +_Noreturn void verr(int, const char *, va_list) __fp(2, 0);
  52. +_Noreturn void errx(int, const char *, ...) __fp(2, 3);
  53. +_Noreturn void verrx(int, const char *, va_list) __fp(2, 0);
  54. +
  55. +#undef __fp
  56. #ifdef __cplusplus
  57. }
  58. --- a/include/monetary.h
  59. +++ b/include/monetary.h
  60. @@ -13,8 +13,16 @@ extern "C" {
  61. #include <bits/alltypes.h>
  62. -ssize_t strfmon(char *__restrict, size_t, const char *__restrict, ...);
  63. -ssize_t strfmon_l(char *__restrict, size_t, locale_t, const char *__restrict, ...);
  64. +#if __GNUC__ >= 3
  65. +#define __fsfm(x, y) __attribute__ ((__format__ (__strfmon__, x, y)))
  66. +#else
  67. +#define __fsfm(x, y)
  68. +#endif
  69. +
  70. +ssize_t strfmon(char *__restrict, size_t, const char *__restrict, ...) __fsfm(3, 4);
  71. +ssize_t strfmon_l(char *__restrict, size_t, locale_t, const char *__restrict, ...) __fsfm(4, 5);
  72. +
  73. +#undef __fsfm
  74. #ifdef __cplusplus
  75. }
  76. --- a/include/stdio.h
  77. +++ b/include/stdio.h
  78. @@ -21,6 +21,14 @@ extern "C" {
  79. #include <bits/alltypes.h>
  80. +#if __GNUC__ >= 3
  81. +#define __fp(x, y) __attribute__ ((__format__ (__printf__, x, y)))
  82. +#define __fs(x, y) __attribute__ ((__format__ (__scanf__, x, y)))
  83. +#else
  84. +#define __fp(x, y)
  85. +#define __fs(x, y)
  86. +#endif
  87. +
  88. #ifdef __cplusplus
  89. #define NULL 0L
  90. #else
  91. @@ -102,19 +110,19 @@ int puts(const char *);
  92. int printf(const char *__restrict, ...);
  93. int fprintf(FILE *__restrict, const char *__restrict, ...);
  94. int sprintf(char *__restrict, const char *__restrict, ...);
  95. -int snprintf(char *__restrict, size_t, const char *__restrict, ...);
  96. +int snprintf(char *__restrict, size_t, const char *__restrict, ...) __fp(3, 4);
  97. int vprintf(const char *__restrict, __isoc_va_list);
  98. int vfprintf(FILE *__restrict, const char *__restrict, __isoc_va_list);
  99. int vsprintf(char *__restrict, const char *__restrict, __isoc_va_list);
  100. -int vsnprintf(char *__restrict, size_t, const char *__restrict, __isoc_va_list);
  101. +int vsnprintf(char *__restrict, size_t, const char *__restrict, __isoc_va_list) __fp(3, 0);
  102. int scanf(const char *__restrict, ...);
  103. int fscanf(FILE *__restrict, const char *__restrict, ...);
  104. int sscanf(const char *__restrict, const char *__restrict, ...);
  105. -int vscanf(const char *__restrict, __isoc_va_list);
  106. -int vfscanf(FILE *__restrict, const char *__restrict, __isoc_va_list);
  107. -int vsscanf(const char *__restrict, const char *__restrict, __isoc_va_list);
  108. +int vscanf(const char *__restrict, __isoc_va_list) __fs(1, 0);
  109. +int vfscanf(FILE *__restrict, const char *__restrict, __isoc_va_list) __fs(2, 0);
  110. +int vsscanf(const char *__restrict, const char *__restrict, __isoc_va_list) __fs(2, 0);
  111. void perror(const char *);
  112. @@ -135,8 +143,8 @@ int pclose(FILE *);
  113. int fileno(FILE *);
  114. int fseeko(FILE *, off_t, int);
  115. off_t ftello(FILE *);
  116. -int dprintf(int, const char *__restrict, ...);
  117. -int vdprintf(int, const char *__restrict, __isoc_va_list);
  118. +int dprintf(int, const char *__restrict, ...) __fp(2, 3);
  119. +int vdprintf(int, const char *__restrict, __isoc_va_list) __fp(2, 0);
  120. void flockfile(FILE *);
  121. int ftrylockfile(FILE *);
  122. void funlockfile(FILE *);
  123. @@ -175,8 +183,8 @@ int fileno_unlocked(FILE *);
  124. int getw(FILE *);
  125. int putw(int, FILE *);
  126. char *fgetln(FILE *, size_t *);
  127. -int asprintf(char **, const char *, ...);
  128. -int vasprintf(char **, const char *, __isoc_va_list);
  129. +int asprintf(char **, const char *, ...) __fp(2, 3);
  130. +int vasprintf(char **, const char *, __isoc_va_list) __fp(2, 0);
  131. #endif
  132. #ifdef _GNU_SOURCE
  133. @@ -184,6 +192,9 @@ char *fgets_unlocked(char *, int, FILE *
  134. int fputs_unlocked(const char *, FILE *);
  135. #endif
  136. +#undef __fp
  137. +#undef __fs
  138. +
  139. #if defined(_LARGEFILE64_SOURCE) || defined(_GNU_SOURCE)
  140. #define tmpfile64 tmpfile
  141. #define fopen64 fopen
  142. --- a/include/syslog.h
  143. +++ b/include/syslog.h
  144. @@ -56,16 +56,22 @@ extern "C" {
  145. #define LOG_NOWAIT 0x10
  146. #define LOG_PERROR 0x20
  147. +#if __GNUC__ >= 3
  148. +#define __fp(x, y) __attribute__ ((__format__ (__printf__, x, y)))
  149. +#else
  150. +#define __fp(x, y)
  151. +#endif
  152. +
  153. void closelog (void);
  154. void openlog (const char *, int, int);
  155. int setlogmask (int);
  156. -void syslog (int, const char *, ...);
  157. +void syslog (int, const char *, ...) __fp(2, 3);
  158. #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
  159. #define _PATH_LOG "/dev/log"
  160. #define __NEED_va_list
  161. #include <bits/alltypes.h>
  162. -void vsyslog (int, const char *, va_list);
  163. +void vsyslog (int, const char *, va_list) __fp(2, 0);
  164. #if defined(SYSLOG_NAMES)
  165. #define INTERNAL_NOPRI 0x10
  166. #define INTERNAL_MARK (LOG_NFACILITIES<<3)
  167. @@ -93,6 +99,8 @@ typedef struct {
  168. #endif
  169. #endif
  170. +#undef __fp
  171. +
  172. #ifdef __cplusplus
  173. }
  174. #endif