Skip to content

Commit 62a7400

Browse files
deronnaxvstinner
andauthored
gh-162054: No longer use _vsnprintf() in PyOS_vsnprintf() (#162053)
Use vsnprintf() on all platforms in PyOS_vsnprintf(). Since Visual Studio 2015, the MSVC runtime provides a C11-conforming vsnprintf(), so the _vsnprintf() special case is no longer needed. Add a test for the truncation case and update the outdated comment. Co-authored-by: Victor Stinner <vstinner@python.org>
1 parent ad2b0ac commit 62a7400

3 files changed

Lines changed: 15 additions & 15 deletions

File tree

‎Lib/test/test_ctypes/test_python_api.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ def test_PyOS_snprintf(self):
6868
PyOS_snprintf(buf, sizeof(buf), b"Hello from %s (%d, %d, %d)", b"ctypes", 1, 2, 3)
6969
self.assertEqual(buf.value, b"Hello from ctypes (1, 2, 3)")
7070

71+
# Test truncated output
72+
small = create_string_buffer(14)
73+
rv = PyOS_snprintf(small, sizeof(small), b"Hello from %s", b"ctypes")
74+
self.assertEqual(rv, len(b"Hello from ctypes"))
75+
self.assertEqual(small.raw, b"Hello from ct\0")
76+
7177
# not enough arguments
7278
self.assertRaises(TypeError, PyOS_snprintf, buf)
7379

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
:c:func:`PyOS_vsnprintf` now calls the standard C11 :c:func:`!vsnprintf` on Windows, instead of
2+
the non-standard :c:func:`!_vsnprintf`. When the output is truncated, it now
3+
returns the number of bytes that would have been written (excluding the null byte), as on other
4+
platforms, instead of a negative value.

‎Python/mysnprintf.c‎

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@
22

33
/* snprintf() and vsnprintf() wrappers.
44
5-
If the platform has vsnprintf, we use it, else we
6-
emulate it in a half-hearted way. Even if the platform has it, we wrap
7-
it because platforms differ in what vsnprintf does in case the buffer
8-
is too small: C99 behavior is to return the number of characters that
9-
would have been written had the buffer not been too small, and to set
10-
the last byte of the buffer to \0. At least MS _vsnprintf returns a
11-
negative value instead, and fills the entire buffer with non-\0 data.
5+
We wrap vsnprintf() to guarantee consistent behavior in corner cases:
6+
C99 behavior is to return the number of characters that would have been
7+
written had the buffer not been too small, and to set the last byte of the
8+
buffer to \0.
129
Unlike C99, our wrappers do not support passing a null buffer.
1310
1411
The wrappers ensure that str[size-1] is always \0 upon return.
@@ -28,10 +25,7 @@
2825
2926
When rv < 0, "something bad happened". str[size-1] is \0 in this
3027
case too, but the rest of str is unreliable. It could be that
31-
an error in format codes was detected by libc, or on platforms
32-
with a non-C99 vsnprintf simply that the buffer wasn't big enough
33-
to avoid truncation, or on platforms without any vsnprintf that
34-
PyMem_Malloc couldn't obtain space for a temp buffer.
28+
an error in format codes was detected by libc.
3529
3630
CAUTION: Unlike C99, str != NULL and size > 0 are required.
3731
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)
6660
goto Done;
6761
}
6862

69-
#if defined(_MSC_VER)
70-
len = _vsnprintf(str, size, format, va);
71-
#else
7263
len = vsnprintf(str, size, format, va);
73-
#endif
7464

7565
Done:
7666
if (size > 0) {

0 commit comments

Comments
 (0)