1 #include "Python.h"
2
3 /* snprintf() and vsnprintf() wrappers.
4
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.
12
13 The wrappers ensure that str[size-1] is always \0 upon return.
14
15 PyOS_snprintf and PyOS_vsnprintf never write more than size bytes
16 (including the trailing '\0') into str.
17
18 If the platform doesn't have vsnprintf, and the buffer size needed to
19 avoid truncation exceeds size by more than 512, Python aborts with a
20 Py_FatalError.
21
22 Return value (rv):
23
24 When 0 <= rv < size, the output conversion was unexceptional, and
25 rv characters were written to str (excluding a trailing \0 byte at
26 str[rv]).
27
28 When rv >= size, output conversion was truncated, and a buffer of
29 size rv+1 would have been needed to avoid truncation. str[size-1]
30 is \0 in this case.
31
32 When rv < 0, "something bad happened". str[size-1] is \0 in this
33 case too, but the rest of str is unreliable. It could be that
34 an error in format codes was detected by libc, or on platforms
35 with a non-C99 vsnprintf simply that the buffer wasn't big enough
36 to avoid truncation, or on platforms without any vsnprintf that
37 PyMem_Malloc couldn't obtain space for a temp buffer.
38
39 CAUTION: Unlike C99, str != NULL and size > 0 are required.
40 */
41
42 int
PyOS_snprintf(char * str,size_t size,const char * format,...)43 PyOS_snprintf(char *str, size_t size, const char *format, ...)
44 {
45 int rc;
46 va_list va;
47
48 va_start(va, format);
49 rc = PyOS_vsnprintf(str, size, format, va);
50 va_end(va);
51 return rc;
52 }
53
54 int
PyOS_vsnprintf(char * str,size_t size,const char * format,va_list va)55 PyOS_vsnprintf(char *str, size_t size, const char *format, va_list va)
56 {
57 assert(str != NULL);
58 assert(size > 0);
59 assert(format != NULL);
60
61 int len; /* # bytes written, excluding \0 */
62 #if defined(_MSC_VER) || defined(HAVE_SNPRINTF)
63 # define _PyOS_vsnprintf_EXTRA_SPACE 1
64 #else
65 # define _PyOS_vsnprintf_EXTRA_SPACE 512
66 char *buffer;
67 #endif
68 /* We take a size_t as input but return an int. Sanity check
69 * our input so that it won't cause an overflow in the
70 * vsnprintf return value or the buffer malloc size. */
71 if (size > INT_MAX - _PyOS_vsnprintf_EXTRA_SPACE) {
72 len = -666;
73 goto Done;
74 }
75
76 #if defined(_MSC_VER)
77 len = _vsnprintf(str, size, format, va);
78 #elif defined(HAVE_SNPRINTF)
79 len = vsnprintf(str, size, format, va);
80 #else
81 /* Emulate vsnprintf(). */
82 buffer = PyMem_MALLOC(size + _PyOS_vsnprintf_EXTRA_SPACE);
83 if (buffer == NULL) {
84 len = -666;
85 goto Done;
86 }
87
88 len = vsprintf(buffer, format, va);
89 if (len < 0) {
90 /* ignore the error */;
91 }
92 else if ((size_t)len >= size + _PyOS_vsnprintf_EXTRA_SPACE) {
93 _Py_FatalErrorFunc(__func__, "Buffer overflow");
94 }
95 else {
96 const size_t to_copy = (size_t)len < size ?
97 (size_t)len : size - 1;
98 assert(to_copy < size);
99 memcpy(str, buffer, to_copy);
100 str[to_copy] = '\0';
101 }
102 PyMem_FREE(buffer);
103 #endif
104
105 Done:
106 if (size > 0) {
107 str[size-1] = '\0';
108 }
109 return len;
110 #undef _PyOS_vsnprintf_EXTRA_SPACE
111 }
112