diff --git a/Lib/test/test_ctypes/test_python_api.py b/Lib/test/test_ctypes/test_python_api.py index 1072a109833261..967dd41bd71ce4 100644 --- a/Lib/test/test_ctypes/test_python_api.py +++ b/Lib/test/test_ctypes/test_python_api.py @@ -68,6 +68,12 @@ def test_PyOS_snprintf(self): PyOS_snprintf(buf, sizeof(buf), b"Hello from %s (%d, %d, %d)", b"ctypes", 1, 2, 3) self.assertEqual(buf.value, b"Hello from ctypes (1, 2, 3)") + # truncated output: C99 return value and trailing NUL + small = create_string_buffer(b"x" * 8, 8) + rv = PyOS_snprintf(small, sizeof(small), b"Hello from %s", b"ctypes") + self.assertEqual(rv, len(b"Hello from ctypes")) + self.assertEqual(small.raw, b"Hello f\0") + # not enough arguments self.assertRaises(TypeError, PyOS_snprintf, buf) diff --git a/Misc/NEWS.d/next/C_API/2026-11-15-12-00-00.gh-issue-162054.vSnPrf.rst b/Misc/NEWS.d/next/C_API/2026-11-15-12-00-00.gh-issue-162054.vSnPrf.rst new file mode 100644 index 00000000000000..9199881fa500b6 --- /dev/null +++ b/Misc/NEWS.d/next/C_API/2026-11-15-12-00-00.gh-issue-162054.vSnPrf.rst @@ -0,0 +1,4 @@ +:c:func:`PyOS_vsnprintf` now calls :c:func:`!vsnprintf` on Windows, instead of +the non-standard :c:func:`!_vsnprintf`. When the output is truncated, it now +returns the number of characters that would have been written, as on other +platforms, instead of a negative value. diff --git a/Python/mysnprintf.c b/Python/mysnprintf.c index 2a505d14f82c99..69ef70de14d375 100644 --- a/Python/mysnprintf.c +++ b/Python/mysnprintf.c @@ -2,13 +2,10 @@ /* snprintf() and vsnprintf() wrappers. - If the platform has vsnprintf, we use it, else we - emulate it in a half-hearted way. Even if the platform has it, we wrap - it because platforms differ in what vsnprintf does in case the buffer - is too small: C99 behavior is to return the number of characters that - would have been written had the buffer not been too small, and to set - the last byte of the buffer to \0. At least MS _vsnprintf returns a - negative value instead, and fills the entire buffer with non-\0 data. + We wrap vsnprintf() to guarantee consistent behavior in corner cases: + C99 behavior is to return the number of characters that would have been + written had the buffer not been too small, and to set the last byte of the + buffer to \0. Unlike C99, our wrappers do not support passing a null buffer. The wrappers ensure that str[size-1] is always \0 upon return. @@ -28,10 +25,7 @@ When rv < 0, "something bad happened". str[size-1] is \0 in this case too, but the rest of str is unreliable. It could be that - an error in format codes was detected by libc, or on platforms - with a non-C99 vsnprintf simply that the buffer wasn't big enough - to avoid truncation, or on platforms without any vsnprintf that - PyMem_Malloc couldn't obtain space for a temp buffer. + an error in format codes was detected by libc. CAUTION: Unlike C99, str != NULL and size > 0 are required. Also, size must be smaller than INT_MAX. @@ -66,11 +60,7 @@ PyOS_vsnprintf(char *str, size_t size, const char *format, va_list va) goto Done; } -#if defined(_MSC_VER) - len = _vsnprintf(str, size, format, va); -#else len = vsnprintf(str, size, format, va); -#endif Done: if (size > 0) {