Building a testdir of 'strtof' on mingw with -D__USE_MINGW_ANSI_STDIO=0, I see
this test failure:

FAIL: test-strerror_r
=====================

../../gltests/test-strerror_r.c:118: assertion 'buf[i - 1] == '\0'' failed
FAIL test-strerror_r.exe (exit status: 3)

The string "Unknown error -3", for buflen == 2, gets truncated to "Un"
without trailing NUL. It should better be truncated to "U" with a trailing
NUL.

This patch fixes it.


2025-09-16  Bruno Haible  <[email protected]>

        strerror_r: Ensure a trailing NUL when truncating.
        * lib/strerror_r.c (strerror_r): Fix use of snprintf.

diff --git a/lib/strerror_r.c b/lib/strerror_r.c
index 9c0161cd01..b67684ebe6 100644
--- a/lib/strerror_r.c
+++ b/lib/strerror_r.c
@@ -450,10 +450,11 @@ strerror_r (int errnum, char *buf, size_t buflen)
 #endif
 #if defined __HAIKU__
         /* For consistency with perror().  */
-        snprintf (buf, buflen, "Unknown Application Error (%d)", errnum);
+        snprintf (buf, buflen - 1, "Unknown Application Error (%d)", errnum);
 #else
-        snprintf (buf, buflen, "Unknown error %d", errnum);
+        snprintf (buf, buflen - 1, "Unknown error %d", errnum);
 #endif
+        buf[buflen - 1] = '\0';
       }
 
     errno = saved_errno;




Reply via email to