blob: a08e249b42b1ef25a268c6f6c500d7aacc6227ce [file] [log] [blame]
Marc-André Lemburge5006eb2001-07-31 13:24:44 +00001#include "Python.h"
Marc-André Lemburge5006eb2001-07-31 13:24:44 +00002
Tim Petersfaad5ad2001-12-03 00:43:33 +00003/* snprintf() wrappers. If the platform has vsnprintf, we use it, else we
4 emulate it in a half-hearted way. Even if the platform has it, we wrap
5 it because platforms differ in what vsnprintf does in case the buffer
6 is too small: C99 behavior is to return the number of characters that
7 would have been written had the buffer not been too small, and to set
8 the last byte of the buffer to \0. At least MS _vsnprintf returns a
9 negative value instead, and fills the entire buffer with non-\0 data.
10
11 The wrappers ensure that str[size-1] is always \0 upon return.
12
13 PyOS_snprintf and PyOS_vsnprintf never write more than size bytes
14 (including the trailing '\0') into str.
15
16 If the platform doesn't have vsnprintf, and the buffer size needed to
17 avoid truncation exceeds size by more than 512, Python aborts with a
18 Py_FatalError.
19
20 Return value (rv):
21
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000022 When 0 <= rv < size, the output conversion was unexceptional, and
23 rv characters were written to str (excluding a trailing \0 byte at
24 str[rv]).
Tim Petersfaad5ad2001-12-03 00:43:33 +000025
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000026 When rv >= size, output conversion was truncated, and a buffer of
27 size rv+1 would have been needed to avoid truncation. str[size-1]
28 is \0 in this case.
Tim Petersfaad5ad2001-12-03 00:43:33 +000029
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000030 When rv < 0, "something bad happened". str[size-1] is \0 in this
31 case too, but the rest of str is unreliable. It could be that
32 an error in format codes was detected by libc, or on platforms
33 with a non-C99 vsnprintf simply that the buffer wasn't big enough
34 to avoid truncation, or on platforms without any vsnprintf that
35 PyMem_Malloc couldn't obtain space for a temp buffer.
Tim Petersfaad5ad2001-12-03 00:43:33 +000036
37 CAUTION: Unlike C99, str != NULL and size > 0 are required.
38*/
39
40int
41PyOS_snprintf(char *str, size_t size, const char *format, ...)
42{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000043 int rc;
44 va_list va;
Tim Petersfaad5ad2001-12-03 00:43:33 +000045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000046 va_start(va, format);
47 rc = PyOS_vsnprintf(str, size, format, va);
48 va_end(va);
49 return rc;
Tim Petersfaad5ad2001-12-03 00:43:33 +000050}
51
52int
53PyOS_vsnprintf(char *str, size_t size, const char *format, va_list va)
54{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000055 int len; /* # bytes written, excluding \0 */
Georg Brandlf78e02b2008-06-10 17:40:04 +000056#ifdef HAVE_SNPRINTF
57#define _PyOS_vsnprintf_EXTRA_SPACE 1
58#else
59#define _PyOS_vsnprintf_EXTRA_SPACE 512
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 char *buffer;
Tim Petersfaad5ad2001-12-03 00:43:33 +000061#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 assert(str != NULL);
63 assert(size > 0);
64 assert(format != NULL);
65 /* We take a size_t as input but return an int. Sanity check
66 * our input so that it won't cause an overflow in the
67 * vsnprintf return value or the buffer malloc size. */
68 if (size > INT_MAX - _PyOS_vsnprintf_EXTRA_SPACE) {
69 len = -666;
70 goto Done;
71 }
Marc-André Lemburge5006eb2001-07-31 13:24:44 +000072
Tim Petersfaad5ad2001-12-03 00:43:33 +000073#ifdef HAVE_SNPRINTF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 len = vsnprintf(str, size, format, va);
Marc-André Lemburge5006eb2001-07-31 13:24:44 +000075#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 /* Emulate it. */
77 buffer = PyMem_MALLOC(size + _PyOS_vsnprintf_EXTRA_SPACE);
78 if (buffer == NULL) {
79 len = -666;
80 goto Done;
81 }
Marc-André Lemburge5006eb2001-07-31 13:24:44 +000082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 len = vsprintf(buffer, format, va);
84 if (len < 0)
85 /* ignore the error */;
Marc-André Lemburge5006eb2001-07-31 13:24:44 +000086
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000087 else if ((size_t)len >= size + _PyOS_vsnprintf_EXTRA_SPACE)
88 Py_FatalError("Buffer overflow in PyOS_snprintf/PyOS_vsnprintf");
Tim Petersfaad5ad2001-12-03 00:43:33 +000089
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000090 else {
91 const size_t to_copy = (size_t)len < size ?
92 (size_t)len : size - 1;
93 assert(to_copy < size);
94 memcpy(str, buffer, to_copy);
95 str[to_copy] = '\0';
96 }
97 PyMem_FREE(buffer);
Marc-André Lemburge5006eb2001-07-31 13:24:44 +000098#endif
Georg Brandlf78e02b2008-06-10 17:40:04 +000099Done:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 if (size > 0)
101 str[size-1] = '\0';
102 return len;
Georg Brandlf78e02b2008-06-10 17:40:04 +0000103#undef _PyOS_vsnprintf_EXTRA_SPACE
Marc-André Lemburge5006eb2001-07-31 13:24:44 +0000104}