diff options
| author | jvoisin | 2024-09-06 13:36:15 +0200 |
|---|---|---|
| committer | jvoisin | 2024-09-06 13:38:22 +0200 |
| commit | f2e7f24daaa43c0927130b6ed02c3ed17689b3ca (patch) | |
| tree | 360f799b6b3fa5a6e5f4837980e5f9831b6c5a64 | |
| parent | 114b563adc2b942bc5abd4c5820507076d453f64 (diff) | |
Work around a gcc warning
It seems that annotating sprintf with `write` makes gcc unhappy, as its
analyser is unable to understand that we're checking if `__b != -1` before
calling `__orig_snprintf`, so let's comment this annotation for now.
| -rw-r--r-- | include/stdio.h | 14 | ||||
| -rw-r--r-- | tests/Makefile | 2 | ||||
| -rw-r--r-- | tests/test_sprintf.c | 8 | ||||
| -rw-r--r-- | tests/test_sprintf_62.c | 21 |
4 files changed, 38 insertions, 7 deletions
diff --git a/include/stdio.h b/include/stdio.h index c1070a8..3e31f29 100644 --- a/include/stdio.h +++ b/include/stdio.h | |||
| @@ -286,7 +286,7 @@ _FORTIFY_FN(snprintf) int snprintf(char *__s, size_t __n, | |||
| 286 | } | 286 | } |
| 287 | 287 | ||
| 288 | __fh_format(printf, 2, 3) | 288 | __fh_format(printf, 2, 3) |
| 289 | __fh_access(write_only, 1) | 289 | //__fh_access(write_only, 1) |
| 290 | __fh_access(read_only, 2) | 290 | __fh_access(read_only, 2) |
| 291 | _FORTIFY_FN(sprintf) int sprintf(char *__s, const char *__f, ...) | 291 | _FORTIFY_FN(sprintf) int sprintf(char *__s, const char *__f, ...) |
| 292 | { | 292 | { |
| @@ -296,13 +296,13 @@ _FORTIFY_FN(sprintf) int sprintf(char *__s, const char *__f, ...) | |||
| 296 | __fh_size_t __b = __fh_bos(__s, 0); | 296 | __fh_size_t __b = __fh_bos(__s, 0); |
| 297 | int __r; | 297 | int __r; |
| 298 | 298 | ||
| 299 | if (__b != (__fh_size_t)-1) { | 299 | if (__b == (__fh_size_t)-1) { |
| 300 | __r = __orig_snprintf(__s, __b, __f, __builtin_va_arg_pack()); | 300 | return __orig_sprintf(__s, __f, __builtin_va_arg_pack()); |
| 301 | if (__r != -1 && (__fh_size_t)__r >= __b) | ||
| 302 | __builtin_trap(); | ||
| 303 | } else { | ||
| 304 | __r = __orig_sprintf(__s, __f, __builtin_va_arg_pack()); | ||
| 305 | } | 301 | } |
| 302 | |||
| 303 | __r = __orig_snprintf(__s, __b, __f, __builtin_va_arg_pack()); | ||
| 304 | if (__r != -1 && (__fh_size_t)__r >= __b) | ||
| 305 | __builtin_trap(); | ||
| 306 | return __r; | 306 | return __r; |
| 307 | #endif | 307 | #endif |
| 308 | } | 308 | } |
diff --git a/tests/Makefile b/tests/Makefile index d29a87d..333f088 100644 --- a/tests/Makefile +++ b/tests/Makefile | |||
| @@ -96,6 +96,8 @@ RUNTIME_TARGETS= \ | |||
| 96 | test_send_static \ | 96 | test_send_static \ |
| 97 | test_sendto_dynamic \ | 97 | test_sendto_dynamic \ |
| 98 | test_sendto_static \ | 98 | test_sendto_static \ |
| 99 | test_sprintf \ | ||
| 100 | test_sprintf_62 \ | ||
| 99 | test_stpcpy_dynamic_write \ | 101 | test_stpcpy_dynamic_write \ |
| 100 | test_stpcpy_overwrite_over \ | 102 | test_stpcpy_overwrite_over \ |
| 101 | test_stpcpy_overwrite_under \ | 103 | test_stpcpy_overwrite_under \ |
diff --git a/tests/test_sprintf.c b/tests/test_sprintf.c new file mode 100644 index 0000000..edde838 --- /dev/null +++ b/tests/test_sprintf.c | |||
| @@ -0,0 +1,8 @@ | |||
| 1 | #include "common.h" | ||
| 2 | |||
| 3 | #include <stdio.h> | ||
| 4 | |||
| 5 | int main(int argc, char** argv) { | ||
| 6 | char buffer[12] = {0}; | ||
| 7 | sprintf(buffer, "%s", "1234567"); | ||
| 8 | } | ||
diff --git a/tests/test_sprintf_62.c b/tests/test_sprintf_62.c new file mode 100644 index 0000000..3a84a1f --- /dev/null +++ b/tests/test_sprintf_62.c | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | #include "common.h" | ||
| 2 | |||
| 3 | #include <stdio.h> | ||
| 4 | |||
| 5 | static char *offstr(char *str) | ||
| 6 | { | ||
| 7 | int len = 0; | ||
| 8 | |||
| 9 | len = sprintf(str, "%s+0x%lx", "foo", (long unsigned int)0); | ||
| 10 | sprintf(str+len, " (%s+0x%lx)","bar", (long unsigned int)0); | ||
| 11 | if (len < 0) | ||
| 12 | return NULL; | ||
| 13 | return str; | ||
| 14 | } | ||
| 15 | |||
| 16 | int main() { | ||
| 17 | char buf[100]; | ||
| 18 | char *c = offstr(buf); | ||
| 19 | printf("%s\n", c); | ||
| 20 | return 0; | ||
| 21 | } | ||
