diff options
| author | jvoisin | 2024-03-19 18:35:13 +0100 |
|---|---|---|
| committer | jvoisin | 2024-03-19 18:35:13 +0100 |
| commit | 8ed72e7c1caa0eb4238e649063c26a11720935de (patch) | |
| tree | 24e478a8ebdb9e69eefcea193781a39bc22c21c0 | |
| parent | 140cffbe84a08669d67c3257258d2bb70ff29c3b (diff) | |
Relax our checks for memchr on C11 and above2.2
Since C11:
> This function behaves as if it reads the bytes sequentially and stops as soon
as a matching bytes is found: if the array pointed to by ptr is smaller than
count, but the match is found within the array, the behavior is well-defined.
Reported-by: q66
Diffstat (limited to '')
| -rw-r--r-- | include/string.h | 4 | ||||
| -rw-r--r-- | tests/test_memchr_dynamic_read.c | 2 | ||||
| -rw-r--r-- | tests/test_memchr_static_read.c | 2 |
3 files changed, 7 insertions, 1 deletions
diff --git a/include/string.h b/include/string.h index 24c1501..db2e6c3 100644 --- a/include/string.h +++ b/include/string.h | |||
| @@ -123,10 +123,12 @@ _FORTIFY_FN(memchr) void *memchr(const void * _FORTIFY_POS0 __d, int __c, size_t | |||
| 123 | if (!__d) | 123 | if (!__d) |
| 124 | __builtin_trap(); | 124 | __builtin_trap(); |
| 125 | 125 | ||
| 126 | #if __STDC_VERSION__ < 201112L | ||
| 126 | __fh_size_t __b = __fh_bos(__d, 0); | 127 | __fh_size_t __b = __fh_bos(__d, 0); |
| 127 | |||
| 128 | if (__n > __b) | 128 | if (__n > __b) |
| 129 | __builtin_trap(); | 129 | __builtin_trap(); |
| 130 | #endif | ||
| 131 | |||
| 130 | return __builtin_memchr(__d, __c, __n); | 132 | return __builtin_memchr(__d, __c, __n); |
| 131 | #endif | 133 | #endif |
| 132 | } | 134 | } |
diff --git a/tests/test_memchr_dynamic_read.c b/tests/test_memchr_dynamic_read.c index 2f8d214..8b25a15 100644 --- a/tests/test_memchr_dynamic_read.c +++ b/tests/test_memchr_dynamic_read.c | |||
| @@ -3,6 +3,7 @@ | |||
| 3 | #include <string.h> | 3 | #include <string.h> |
| 4 | 4 | ||
| 5 | int main(int argc, char** argv) { | 5 | int main(int argc, char** argv) { |
| 6 | #if __STDC_VERSION__ < 201112L | ||
| 6 | const char* buffer = "12345"; | 7 | const char* buffer = "12345"; |
| 7 | memchr(buffer, (int)'4', strlen(buffer) - 1); | 8 | memchr(buffer, (int)'4', strlen(buffer) - 1); |
| 8 | puts(buffer); | 9 | puts(buffer); |
| @@ -12,5 +13,6 @@ int main(int argc, char** argv) { | |||
| 12 | CHK_FAIL_END | 13 | CHK_FAIL_END |
| 13 | 14 | ||
| 14 | puts(buffer); | 15 | puts(buffer); |
| 16 | #endif | ||
| 15 | return ret; | 17 | return ret; |
| 16 | } | 18 | } |
diff --git a/tests/test_memchr_static_read.c b/tests/test_memchr_static_read.c index ccf46af..edd266d 100644 --- a/tests/test_memchr_static_read.c +++ b/tests/test_memchr_static_read.c | |||
| @@ -3,6 +3,7 @@ | |||
| 3 | #include <string.h> | 3 | #include <string.h> |
| 4 | 4 | ||
| 5 | int main(int argc, char** argv) { | 5 | int main(int argc, char** argv) { |
| 6 | #if __STDC_VERSION__ < 201112L | ||
| 6 | const char* buffer = "12345"; | 7 | const char* buffer = "12345"; |
| 7 | const char* padding = "ABCDEFGHIJKLMN"; | 8 | const char* padding = "ABCDEFGHIJKLMN"; |
| 8 | memchr(buffer, (int)'4', strlen(buffer) - 1); | 9 | memchr(buffer, (int)'4', strlen(buffer) - 1); |
| @@ -13,5 +14,6 @@ int main(int argc, char** argv) { | |||
| 13 | CHK_FAIL_END | 14 | CHK_FAIL_END |
| 14 | 15 | ||
| 15 | puts(buffer); | 16 | puts(buffer); |
| 17 | #endif | ||
| 16 | return ret; | 18 | return ret; |
| 17 | } | 19 | } |
