Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Lib/test/test_ctypes/test_python_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
20 changes: 5 additions & 15 deletions Python/mysnprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down
Loading