fortify-headers

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

commit 6040b4a27409968c764353a98c45d972cfd89a8a
parent b9121e4d679b5fccb69abcb58b05a6be5c7191dc
Author: jvoisin <julien.voisin@dustri.org>
Date:   Thu, 30 Apr 2026 17:37:02 +0200

Fix a bug in stpncpy

The manpage says that stpncpy will "copy non-null bytes from the string pointed
to by src into the array pointed to by dst.", it doesn't add a terminal NULL
byte, so we shouldn't check for it.

Diffstat:
Minclude/string.h | 2+-
Mtests/test_stpncpy_static_write.c | 3++-
2 files changed, 3 insertions(+), 2 deletions(-)

diff --git a/include/string.h b/include/string.h @@ -104,7 +104,7 @@ _FORTIFY_FN(stpncpy) char *stpncpy(char * _FORTIFY_POS0 __d, const char *__s, { size_t __b = __bos(__d, 0); - if (__n > __b && strlen(__s) + 1 > __b) + if (__n > __b) __builtin_trap(); return __orig_stpncpy(__d, __s, __n); } diff --git a/tests/test_stpncpy_static_write.c b/tests/test_stpncpy_static_write.c @@ -4,7 +4,8 @@ int main(int argc, char** argv) { char buffer[8] = {0}; - stpncpy(buffer, "1234567", 5); + stpncpy(buffer, "1234567890", 5); + stpncpy(buffer, "1234567890", 8); puts(buffer); CHK_FAIL_START