diff options
author | Guido van Rossum <guido@python.org> | 2001-12-01 16:00:10 +0000 |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2001-12-01 16:00:10 +0000 |
commit | 77b00cd76b2114fd3cd74a516dafe873947ae17b (patch) | |
tree | b3f672e498a05bb13d33650c9e31afeff84cf147 /Python/mysnprintf.c | |
parent | 43b14931c864c346d083d753c0b6c2e0576754d8 (diff) | |
download | cpython-77b00cd76b2114fd3cd74a516dafe873947ae17b.tar.gz |
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.
Diffstat (limited to 'Python/mysnprintf.c')
-rw-r--r-- | Python/mysnprintf.c | 10 |
1 files changed, 5 insertions, 5 deletions
diff --git a/Python/mysnprintf.c b/Python/mysnprintf.c index a373f4efe6..02f929137a 100644 --- a/Python/mysnprintf.c +++ b/Python/mysnprintf.c @@ -40,11 +40,11 @@ int myvsnprintf(char *str, size_t size, const char *format, va_list va) assert(len >= 0); if ((size_t)len > size + 512) Py_FatalError("Buffer overflow in PyOS_snprintf/PyOS_vsnprintf"); - if ((size_t)len > size) { - PyMem_Free(buffer); - return len - 1; - } - memcpy(str, buffer, len); + if ((size_t)len > size) + buffer[size-1] = '\0'; + else + size = len; + memcpy(str, buffer, size); PyMem_Free(buffer); return len - 1; } |