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.
This commit is contained in:
parent
82285dad8e
commit
64be0b4aa5
|
@ -40,11 +40,11 @@ int myvsnprintf(char *str, size_t size, const char *format, va_list va)
|
||||||
assert(len >= 0);
|
assert(len >= 0);
|
||||||
if ((size_t)len > size + 512)
|
if ((size_t)len > size + 512)
|
||||||
Py_FatalError("Buffer overflow in PyOS_snprintf/PyOS_vsnprintf");
|
Py_FatalError("Buffer overflow in PyOS_snprintf/PyOS_vsnprintf");
|
||||||
if ((size_t)len > size) {
|
if ((size_t)len > size)
|
||||||
PyMem_Free(buffer);
|
buffer[size-1] = '\0';
|
||||||
return len - 1;
|
else
|
||||||
}
|
size = len;
|
||||||
memcpy(str, buffer, len);
|
memcpy(str, buffer, size);
|
||||||
PyMem_Free(buffer);
|
PyMem_Free(buffer);
|
||||||
return len - 1;
|
return len - 1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue