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