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