Mercurial > cpython-lru_cache-weakref
comparison Python/mysnprintf.c @ 21126:6b6a9cae256e legacy-trunk
When the number of bytes written to the malloc'ed buffer is larger
than the argument string size, copy as many bytes as will fit
(including a terminating '\0'), rather than not copying anything.
This to make it satisfy the C99 spec.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Sat, 01 Dec 2001 16:00:10 +0000 |
| parents | 2bc648932d44 |
| children | 87fa6e9bd77e |
comparison
equal
deleted
inserted
replaced
| 21125:17e87aa37796 | 21126:6b6a9cae256e |
|---|---|
| 38 } | 38 } |
| 39 len++; | 39 len++; |
| 40 assert(len >= 0); | 40 assert(len >= 0); |
| 41 if ((size_t)len > size + 512) | 41 if ((size_t)len > size + 512) |
| 42 Py_FatalError("Buffer overflow in PyOS_snprintf/PyOS_vsnprintf"); | 42 Py_FatalError("Buffer overflow in PyOS_snprintf/PyOS_vsnprintf"); |
| 43 if ((size_t)len > size) { | 43 if ((size_t)len > size) |
| 44 PyMem_Free(buffer); | 44 buffer[size-1] = '\0'; |
| 45 return len - 1; | 45 else |
| 46 } | 46 size = len; |
| 47 memcpy(str, buffer, len); | 47 memcpy(str, buffer, size); |
| 48 PyMem_Free(buffer); | 48 PyMem_Free(buffer); |
| 49 return len - 1; | 49 return len - 1; |
| 50 } | 50 } |
| 51 | 51 |
| 52 int PyOS_snprintf(char *str, size_t size, const char *format, ...) | 52 int PyOS_snprintf(char *str, size_t size, const char *format, ...) |
