Skip to content

Commit b5b475a

Browse files
author
gvanrossum
committed
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. git-svn-id: http://svn.python.org/projects/python/trunk@24439 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 0eaa542 commit b5b475a

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

Python/mysnprintf.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ int myvsnprintf(char *str, size_t size, const char *format, va_list va)
4040
assert(len >= 0);
4141
if ((size_t)len > size + 512)
4242
Py_FatalError("Buffer overflow in PyOS_snprintf/PyOS_vsnprintf");
43-
if ((size_t)len > size) {
44-
PyMem_Free(buffer);
45-
return len - 1;
46-
}
47-
memcpy(str, buffer, len);
43+
if ((size_t)len > size)
44+
buffer[size-1] = '\0';
45+
else
46+
size = len;
47+
memcpy(str, buffer, size);
4848
PyMem_Free(buffer);
4949
return len - 1;
5050
}

0 commit comments

Comments
 (0)