]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
mprintf: terminate snprintf output on windows
authorJay Satiro <raysatiro@yahoo.com>
Tue, 14 Jan 2025 09:21:38 +0000 (04:21 -0500)
committerJay Satiro <raysatiro@yahoo.com>
Tue, 14 Jan 2025 09:33:51 +0000 (04:33 -0500)
- Null terminate the end of the snprintf output buffer on Windows.

Old versions of the Windows CRT (which are often found on later versions
of Windows) do not terminate the snprintf output buffer if the output
reaches the max size.

This is a follow-up to parent 7e32f656 which made the same change but
limited it to mingw, however it is a CRT version issue irrespective of
compiler.

Ref: https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/snprintf-snprintf-snprintf-l-snwprintf-snwprintf-l?view=msvc-170#remarks

Closes https://github.com/curl/curl/pull/15997

lib/mprintf.c

index 61e352e65cc50de9d8548a599d7f6383a31dc386..8bc9054407c0406cda093816d77ae278bcc0f702 100644 (file)
@@ -1015,6 +1015,11 @@ number:
          output characters */
 #ifdef HAVE_SNPRINTF
       (snprintf)(work, BUFFSIZE, formatbuf, iptr->val.dnum); /* NOLINT */
+#ifdef _WIN32
+      /* Old versions of the Windows CRT do not terminate the snprintf output
+         buffer if it reaches the max size so we do that here. */
+      work[BUFFSIZE - 1] = 0;
+#endif
 #else
       (sprintf)(work, formatbuf, iptr->val.dnum);
 #endif
@@ -1022,11 +1027,6 @@ number:
 #pragma clang diagnostic pop
 #endif
       DEBUGASSERT(strlen(work) < BUFFSIZE);
-#ifdef __MINGW32__
-      /* Work-around for a nasty bug seen with old-mingw and gcc 7.3.0 when it
-         writes one byte more than permitted. */
-      work[BUFFSIZE - 1] = 0;
-#endif
       for(fptr = work; *fptr; fptr++)
         OUTCHAR(*fptr);
       break;