Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 64be0b4

Browse files
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.
1 parent 82285da commit 64be0b4

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)