blob: 458ca14d5c611e08401640dc2d196935437b9b67 [file] [log] [blame]
Marc-André Lemburge5006eb2001-07-31 13:24:44 +00001#include "Python.h"
Marc-André Lemburge5006eb2001-07-31 13:24:44 +00002
Miss Islington (bot)b498c7f2020-06-15 13:20:10 -07003/* snprintf() and vsnprintf() wrappers.
4
5 If the platform has vsnprintf, we use it, else we
Tim Petersfaad5ad2001-12-03 00:43:33 +00006 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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000024 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]).
Tim Petersfaad5ad2001-12-03 00:43:33 +000027
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000028 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.
Tim Petersfaad5ad2001-12-03 00:43:33 +000031
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000032 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.
Tim Petersfaad5ad2001-12-03 00:43:33 +000038
39 CAUTION: Unlike C99, str != NULL and size > 0 are required.
40*/
41
42int
43PyOS_snprintf(char *str, size_t size, const char *format, ...)
44{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 int rc;
46 va_list va;
Tim Petersfaad5ad2001-12-03 00:43:33 +000047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 va_start(va, format);
49 rc = PyOS_vsnprintf(str, size, format, va);
50 va_end(va);
51 return rc;
Tim Petersfaad5ad2001-12-03 00:43:33 +000052}
53
54int
55PyOS_vsnprintf(char *str, size_t size, const char *format, va_list va)
56{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 assert(str != NULL);
58 assert(size > 0);
59 assert(format != NULL);
Miss Islington (bot)b498c7f2020-06-15 13:20:10 -070060
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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 /* 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 }
Marc-André Lemburge5006eb2001-07-31 13:24:44 +000075
Miss Islington (bot)b498c7f2020-06-15 13:20:10 -070076#if defined(_MSC_VER)
77 len = _vsnprintf(str, size, format, va);
78#elif defined(HAVE_SNPRINTF)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 len = vsnprintf(str, size, format, va);
Marc-André Lemburge5006eb2001-07-31 13:24:44 +000080#else
Miss Islington (bot)b498c7f2020-06-15 13:20:10 -070081 /* Emulate vsnprintf(). */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 buffer = PyMem_MALLOC(size + _PyOS_vsnprintf_EXTRA_SPACE);
83 if (buffer == NULL) {
84 len = -666;
85 goto Done;
86 }
Marc-André Lemburge5006eb2001-07-31 13:24:44 +000087
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 len = vsprintf(buffer, format, va);
Victor Stinner87d3b9d2020-03-25 19:27:36 +010089 if (len < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 /* ignore the error */;
Victor Stinner87d3b9d2020-03-25 19:27:36 +010091 }
92 else if ((size_t)len >= size + _PyOS_vsnprintf_EXTRA_SPACE) {
93 _Py_FatalErrorFunc(__func__, "Buffer overflow");
94 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 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);
Marc-André Lemburge5006eb2001-07-31 13:24:44 +0000103#endif
Miss Islington (bot)b498c7f2020-06-15 13:20:10 -0700104
Georg Brandlf78e02b2008-06-10 17:40:04 +0000105Done:
Miss Islington (bot)b498c7f2020-06-15 13:20:10 -0700106 if (size > 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 str[size-1] = '\0';
Miss Islington (bot)b498c7f2020-06-15 13:20:10 -0700108 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 return len;
Georg Brandlf78e02b2008-06-10 17:40:04 +0000110#undef _PyOS_vsnprintf_EXTRA_SPACE
Marc-André Lemburge5006eb2001-07-31 13:24:44 +0000111}