diff 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
line wrap: on
line diff
--- a/Python/mysnprintf.c	Sat Dec 01 04:11:16 2001 +0000
+++ b/Python/mysnprintf.c	Sat Dec 01 16:00:10 2001 +0000
@@ -40,11 +40,11 @@
     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;
 }