summaryrefslogtreecommitdiff
path: root/Objects/bytesobject.c
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2015-10-14 12:02:39 +0200
committerVictor Stinner <victor.stinner@gmail.com>2015-10-14 12:02:39 +0200
commitf6358a7e4c635a74202e73c76a01f5046e1b7c36 (patch)
tree6469b605f7df5233b367534361010e9e3d58e7bc /Objects/bytesobject.c
parentf091033b149792e4084a479444c39636c7be2cad (diff)
downloadcpython-git-f6358a7e4c635a74202e73c76a01f5046e1b7c36.tar.gz
_PyBytesWriter_Alloc(): only use 10 bytes of the small buffer in debug mode to
enhance code to detect buffer under- and overflow.
Diffstat (limited to 'Objects/bytesobject.c')
-rw-r--r--Objects/bytesobject.c14
1 files changed, 13 insertions, 1 deletions
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index 2d4cf4b9ec..88106477d2 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -4053,8 +4053,20 @@ _PyBytesWriter_Alloc(_PyBytesWriter *writer, Py_ssize_t size)
writer->use_small_buffer = 1;
#ifdef Py_DEBUG
- /* the last byte is reserved, it must be '\0' */
writer->allocated = sizeof(writer->small_buffer) - 1;
+ /* In debug mode, don't use the full small buffer because it is less
+ efficient than bytes and bytearray objects to detect buffer underflow
+ and buffer overflow. Use 10 bytes of the small buffer to test also
+ code using the smaller buffer in debug mode.
+
+ Don't modify the _PyBytesWriter structure (use a shorter small buffer)
+ in debug mode to also be able to detect stack overflow when running
+ tests in debug mode. The _PyBytesWriter is large (more than 512 bytes),
+ if Py_EnterRecursiveCall() is not used in deep C callback, we may hit a
+ stack overflow. */
+ writer->allocated = Py_MIN(writer->allocated, 10);
+ /* _PyBytesWriter_CheckConsistency() requires the last byte to be 0,
+ to detect buffer overflow */
writer->small_buffer[writer->allocated] = 0;
#else
writer->allocated = sizeof(writer->small_buffer);