GeezerVison is showing github.com — gh-156939: Detect buffer overflow in PyBytesWriter in debug mode by vstinner · Pull Request #156943 · python/cpython
Conversation
vstinner
commented
Sep 4, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Reserve one byte in PyBytesWriter used as a canary byte: set it to a special value. PyBytesWriter_Finish() checks if the canary byte has been overriden. Add a test on the feature.
Update buffer overflow check in fcntl: allocate extra guard bytes in the writer and then truncate these bytes.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Oh, the test_xml_etree_c failure is a legit failure: unicode_encode_ucs1() calls with errors="xmlcharrefreplace" writes a NUL byte after the writer buffer. xmlcharrefreplace() calls sprintf(str, "&#%d;", PyUnicode_READ(kind, data, i)) which writes a final NUL byte.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
The test_struct error is also a legit bug: a Pascal string of 0 characters writes a NUL byte into an empty buffer, and so triggers a buffer overflow.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
On Ubuntu, 8 tests are failing:
8 tests failed:
test_codeccallbacks test_fcntl test_minidom test_sax test_struct
test_urllib test_xml_etree test_xml_etree_c
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Reserve one byte in PyBytesWriter used as a canary byte: set it to a
special value. PyBytesWriter_Finish() checks if the canary byte has
been overriden. Add a test on the feature.
Update buffer overflow check in fcntl: allocate extra guard bytes in
the writer and then truncate these bytes.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Avoid calling PyBytesWriter_Grow().
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
* Add test_get_data_canary()
* Update test_get_data()
unicode_encode_ucs1() now calls PyBytesWriter_Grow() to update the
PyBytesWriter size.
PyBytesWriter_Grow() no longer calls byteswriter_resize() if grow is
smaller than 0.
Uh oh!
There was an error while loading. Please reload this page.
|
Android x86_64 fails with:
ERROR: test_str (test.test_android.TestAndroidOutput.test_str) [stdout]
----------------------------------------------------------------------
Traceback (most recent call last):
File "/data/user/0/org.python.testbed/files/python/lib/python3.16/test/test_android.py", line 171, in test_str
write("\ud800\udc00", [r"\ud800\udc00"])
~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/data/user/0/org.python.testbed/files/python/lib/python3.16/test/test_android.py", line 148, in write
self.assertEqual(write_len, stream.write(s))
~~~~~~~~~~~~^^^
File "/data/user/0/org.python.testbed/files/python/lib/python3.16/_android_support.py", line 76, in write
self._write_chunk(chunk)
~~~~~~~~~~~~~~~~~^^^^^^^
File "/data/user/0/org.python.testbed/files/python/lib/python3.16/_android_support.py", line 83, in _write_chunk
b = s.encode(self.encoding, self.errors)
ValueError: size larger than allocated size
This error comes from PyBytesWriter_FinishWithSize(): the requested size is too big. I can reproduce it with just:
>>> "\ud800\udc00".encode('utf8', 'backslashreplace')
Traceback (most recent call last):
File "<python-input-3>", line 1, in <module>
"\ud800\udc00".encode('utf8', 'backslashreplace')
~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: size larger than allocated size
Ah! I forgot to update the UTF-8 encoder.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Ah! I forgot to update the UTF-8 encoder.
I wrote PR gh-157383 for that.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
⚠️⚠️⚠️ Buildbot failure ⚠️⚠️⚠️
Hi! The buildbot aarch64 Android 3.x (tier-3) has failed when building commit 12a1de1.
What do you need to do:
- Don't panic.
- Check the buildbot page in the devguide if you don't know what the buildbots are or how they work.
- Go to the page of the buildbot that failed (https://buildbot.python.org/#/builders/1594/builds/5913) and take a look at the build logs.
- Check if the failure is related to this commit (12a1de1) or if it is a false positive.
- If the failure is related to this commit, please, reflect that on the issue and make a new Pull Request with a fix.
You can take a look at the buildbot page here:
https://buildbot.python.org/#/builders/1594/builds/5913
Failed tests:
Failed subtests:
- test_str - test.test_android.TestAndroidOutput.test_str
Summary of the results of the build (if available):
==
Click to see traceback logs
Traceback (most recent call last):
File "/data/user/0/org.python.testbed/files/python/lib/python3.16/test/test_android.py", line 171, in test_str
write("\ud800\udc00", [r"\ud800\udc00"])
~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/data/user/0/org.python.testbed/files/python/lib/python3.16/test/test_android.py", line 148, in write
self.assertEqual(write_len, stream.write(s))
~~~~~~~~~~~~^^^
File "/data/user/0/org.python.testbed/files/python/lib/python3.16/_android_support.py", line 76, in write
self._write_chunk(chunk)
~~~~~~~~~~~~~~~~~^^^^^^^
File "/data/user/0/org.python.testbed/files/python/lib/python3.16/_android_support.py", line 83, in _write_chunk
b = s.encode(self.encoding, self.errors)
ValueError: size larger than allocated size
======================================================================
ERROR: test_str (test.test_android.TestAndroidOutput.test_str) [stderr]
----------------------------------------------------------------------
Traceback (most recent call last):
File "/data/user/0/org.python.testbed/files/python/lib/python3.16/test/test_android.py", line 171, in test_str
write("\ud800\udc00", [r"\ud800\udc00"])
~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/data/user/0/org.python.testbed/files/python/lib/python3.16/test/test_android.py", line 148, in write
self.assertEqual(write_len, stream.write(s))
~~~~~~~~~~~~^^^
File "/data/user/0/org.python.testbed/files/python/lib/python3.16/_android_support.py", line 76, in write
self._write_chunk(chunk)
~~~~~~~~~~~~~~~~~^^^^^^^
File "/data/user/0/org.python.testbed/files/python/lib/python3.16/_android_support.py", line 83, in _write_chunk
b = s.encode(self.encoding, self.errors)
ValueError: size larger than allocated size
----------------------------------------------------------------------
Ran 3 tests in 0.081s
FAILED (errors=2)
Traceback (most recent call last):
File "/data/user/0/org.python.testbed/files/python/lib/python3.16/test/test_android.py", line 171, in test_str
write("\ud800\udc00", [r"\ud800\udc00"])
~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/data/user/0/org.python.testbed/files/python/lib/python3.16/test/test_android.py", line 148, in write
self.assertEqual(write_len, stream.write(s))
~~~~~~~~~~~~^^^
File "/data/user/0/org.python.testbed/files/python/lib/python3.16/_android_support.py", line 76, in write
self._write_chunk(chunk)
~~~~~~~~~~~~~~~~~^^^^^^^
File "/data/user/0/org.python.testbed/files/python/lib/python3.16/_android_support.py", line 83, in _write_chunk
b = s.encode(self.encoding, self.errors)
ValueError: size larger than allocated size
======================================================================
ERROR: test_str (test.test_android.TestAndroidOutput.test_str) [stderr]
----------------------------------------------------------------------
Traceback (most recent call last):
File "/data/user/0/org.python.testbed/files/python/lib/python3.16/test/test_android.py", line 171, in test_str
write("\ud800\udc00", [r"\ud800\udc00"])
~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/data/user/0/org.python.testbed/files/python/lib/python3.16/test/test_android.py", line 148, in write
self.assertEqual(write_len, stream.write(s))
~~~~~~~~~~~~^^^
File "/data/user/0/org.python.testbed/files/python/lib/python3.16/_android_support.py", line 76, in write
self._write_chunk(chunk)
~~~~~~~~~~~~~~~~~^^^^^^^
File "/data/user/0/org.python.testbed/files/python/lib/python3.16/_android_support.py", line 83, in _write_chunk
b = s.encode(self.encoding, self.errors)
ValueError: size larger than allocated size
----------------------------------------------------------------------
Ran 3 tests in 0.134s
FAILED (errors=2)
bytearraymemoryviewmmrve\xc0\x80\xc0\x80�\xc0\x80\xfe\xff\xff\xffaHelloHello world olé中文😀test test_android failed
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
⚠️⚠️⚠️ Buildbot failure ⚠️⚠️⚠️
Hi! The buildbot AMD64 Android 3.x (tier-3) has failed when building commit 12a1de1.
What do you need to do:
- Don't panic.
- Check the buildbot page in the devguide if you don't know what the buildbots are or how they work.
- Go to the page of the buildbot that failed (https://buildbot.python.org/#/builders/1591/builds/5239) and take a look at the build logs.
- Check if the failure is related to this commit (12a1de1) or if it is a false positive.
- If the failure is related to this commit, please, reflect that on the issue and make a new Pull Request with a fix.
You can take a look at the buildbot page here:
https://buildbot.python.org/#/builders/1591/builds/5239
Failed tests:
Failed subtests:
- test_str - test.test_android.TestAndroidOutput.test_str
Summary of the results of the build (if available):
==
Click to see traceback logs
Traceback (most recent call last):
File "/data/user/0/org.python.testbed/files/python/lib/python3.16/test/test_android.py", line 171, in test_str
write("\ud800\udc00", [r"\ud800\udc00"])
~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/data/user/0/org.python.testbed/files/python/lib/python3.16/test/test_android.py", line 148, in write
self.assertEqual(write_len, stream.write(s))
~~~~~~~~~~~~^^^
File "/data/user/0/org.python.testbed/files/python/lib/python3.16/_android_support.py", line 76, in write
self._write_chunk(chunk)
~~~~~~~~~~~~~~~~~^^^^^^^
File "/data/user/0/org.python.testbed/files/python/lib/python3.16/_android_support.py", line 83, in _write_chunk
b = s.encode(self.encoding, self.errors)
ValueError: size larger than allocated size
======================================================================
ERROR: test_str (test.test_android.TestAndroidOutput.test_str) [stderr]
----------------------------------------------------------------------
Traceback (most recent call last):
File "/data/user/0/org.python.testbed/files/python/lib/python3.16/test/test_android.py", line 171, in test_str
write("\ud800\udc00", [r"\ud800\udc00"])
~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/data/user/0/org.python.testbed/files/python/lib/python3.16/test/test_android.py", line 148, in write
self.assertEqual(write_len, stream.write(s))
~~~~~~~~~~~~^^^
File "/data/user/0/org.python.testbed/files/python/lib/python3.16/_android_support.py", line 76, in write
self._write_chunk(chunk)
~~~~~~~~~~~~~~~~~^^^^^^^
File "/data/user/0/org.python.testbed/files/python/lib/python3.16/_android_support.py", line 83, in _write_chunk
b = s.encode(self.encoding, self.errors)
ValueError: size larger than allocated size
----------------------------------------------------------------------
Ran 3 tests in 0.595s
FAILED (errors=2)
bytearraymemoryviewmmrve\xc0\x80\xc0\x80�\xc0\x80\xfe\xff\xff\xffaHelloHello world olé中文😀test test_android failed
Traceback (most recent call last):
File "/data/user/0/org.python.testbed/files/python/lib/python3.16/test/test_android.py", line 171, in test_str
write("\ud800\udc00", [r"\ud800\udc00"])
~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/data/user/0/org.python.testbed/files/python/lib/python3.16/test/test_android.py", line 148, in write
self.assertEqual(write_len, stream.write(s))
~~~~~~~~~~~~^^^
File "/data/user/0/org.python.testbed/files/python/lib/python3.16/_android_support.py", line 76, in write
self._write_chunk(chunk)
~~~~~~~~~~~~~~~~~^^^^^^^
File "/data/user/0/org.python.testbed/files/python/lib/python3.16/_android_support.py", line 83, in _write_chunk
b = s.encode(self.encoding, self.errors)
ValueError: size larger than allocated size
======================================================================
ERROR: test_str (test.test_android.TestAndroidOutput.test_str) [stderr]
----------------------------------------------------------------------
Traceback (most recent call last):
File "/data/user/0/org.python.testbed/files/python/lib/python3.16/test/test_android.py", line 171, in test_str
write("\ud800\udc00", [r"\ud800\udc00"])
~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/data/user/0/org.python.testbed/files/python/lib/python3.16/test/test_android.py", line 148, in write
self.assertEqual(write_len, stream.write(s))
~~~~~~~~~~~~^^^
File "/data/user/0/org.python.testbed/files/python/lib/python3.16/_android_support.py", line 76, in write
self._write_chunk(chunk)
~~~~~~~~~~~~~~~~~^^^^^^^
File "/data/user/0/org.python.testbed/files/python/lib/python3.16/_android_support.py", line 83, in _write_chunk
b = s.encode(self.encoding, self.errors)
ValueError: size larger than allocated size
----------------------------------------------------------------------
Ran 3 tests in 0.228s
FAILED (errors=2)
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
johnslavik
pushed a commit
to johnslavik/cpython
that referenced
this pull request
Sep 14, 2026
python#156943)
Reserve one byte in PyBytesWriter used as a canary byte: set it to a
special value. PyBytesWriter_Finish() checks if the canary byte has
been overriden to detect a buffer overflow. Add tests for this feature.
Update buffer overflow check in fcntl: allocate extra guard bytes in
the writer and then truncate these bytes.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|