fortify-headers

standalone fortify-source implementation
git clone git://git.2f30.org/fortify-headers
Log | Files | Refs | README | LICENSE

commit 8ed72e7c1caa0eb4238e649063c26a11720935de
parent 140cffbe84a08669d67c3257258d2bb70ff29c3b
Author: jvoisin <julien.voisin@dustri.org>
Date:   Tue, 19 Mar 2024 18:35:13 +0100

Relax our checks for memchr on C11 and above

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:
Minclude/string.h | 4+++-
Mtests/test_memchr_dynamic_read.c | 2++
Mtests/test_memchr_static_read.c | 2++
3 files changed, 7 insertions(+), 1 deletion(-)

diff --git 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 if (!__d) __builtin_trap(); +#if __STDC_VERSION__ < 201112L __fh_size_t __b = __fh_bos(__d, 0); - if (__n > __b) __builtin_trap(); +#endif + return __builtin_memchr(__d, __c, __n); #endif } diff --git a/tests/test_memchr_dynamic_read.c b/tests/test_memchr_dynamic_read.c @@ -3,6 +3,7 @@ #include <string.h> int main(int argc, char** argv) { +#if __STDC_VERSION__ < 201112L const char* buffer = "12345"; memchr(buffer, (int)'4', strlen(buffer) - 1); puts(buffer); @@ -12,5 +13,6 @@ int main(int argc, char** argv) { CHK_FAIL_END puts(buffer); +#endif return ret; } diff --git a/tests/test_memchr_static_read.c b/tests/test_memchr_static_read.c @@ -3,6 +3,7 @@ #include <string.h> int main(int argc, char** argv) { +#if __STDC_VERSION__ < 201112L const char* buffer = "12345"; const char* padding = "ABCDEFGHIJKLMN"; memchr(buffer, (int)'4', strlen(buffer) - 1); @@ -13,5 +14,6 @@ int main(int argc, char** argv) { CHK_FAIL_END puts(buffer); +#endif return ret; }