FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-156995: Fix `bytearray.take_bytes()` corrupting shared single-byte bytes objects by StanFromIreland · Pull Request #156996 · python/cpython · GitHub

Repository navigation

gh-156995: Fix bytearray.take_bytes() corrupting shared single-byte bytes objects - #156996

Merged
vstinner merged 5 commits into
python:mainfrom
StanFromIreland:bytearray.take_bytes
Sep 10, 2026
Merged

vstinner merged 5 commits into
python:mainfrom
StanFromIreland:bytearray.take_bytes

Conversation

StanFromIreland commented Sep 5, 2026 •
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Member

cmaloney left a comment •
edited
Loading

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Double checking against implementation this change looks correct.

I think should add an assert as a safety net to help catch this bug into bytearray_reinit_from_bytes which is called every time self->ob_bytes_object is set to re-init.

bytearray_reinit_from_bytes(PyByteArrayObject *self, Py_ssize_t size,
                            Py_ssize_t alloc)
{
    /* Only the empty bytes may be immortal. */
    assert((alloc == 0) == _Py_IsImmortal(self->ob_bytes_object));

That safety net in free-threaded builds finds an additional case that can happen. _PyBytes_Resize uses _PyObject_IsUniquelyReferenced which under free threading includes that the object is owned by the current thread. If another thread swapped out the underlying bytes the unquiely reference returns false. if it is a downsize that calls PyBytes_FromStringAndSize which passes non-NULL to its first parameter...

Not sure the best way to fix that... I'd lean that _PyBytes_Resize should never return an immutable object for size >= 1 as that can lead to a really subtle only under free-threading bug...

Co-authored-by: Cody Maloney <cmaloney@theoreticalchaos.com>

Copy link
Copy Markdown
Member Author

Thanks for the review @cmaloney, and nice catch! I've applied both of your suggestions.

cmaloney left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

cc: @vstinner would be good to get more eyes on the _PyBytes_Resize part in particular as this would be nice to get into 3.15

Copy link
Copy Markdown
Member Author

I agree we should backport, I worry this can be quite nasty, and it's not an unrealistic scenario.

Comment thread Objects/bytearrayobject.c Outdated
Co-authored-by: Cody Maloney <cmaloney@users.noreply.github.com>

vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

The overall change LGTM, but I would prefer to revert the _PyBytes_Resize() change from this PR.

Comment thread Objects/bytesobject.c

vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Please ignore my previous comment: the _PyBytes_Resize() change is needed by this fix. test_bytes fails (in debug mode) without this change.

I suggest to complete the _PyBytes_Resize() C API to check that the result is a fresh object:

diff --git a/Lib/test/test_capi/test_bytes.py b/Lib/test/test_capi/test_bytes.py
index 38cda931e7d..590e118c04e 100644
--- a/Lib/test/test_capi/test_bytes.py
+++ b/Lib/test/test_capi/test_bytes.py
@@ -1,3 +1,4 @@
+import sys
 import unittest
 from test.support import import_helper
 
@@ -231,26 +232,41 @@ def test_decodeescape(self):
 
     def test_resize(self):
         """Test _PyBytes_Resize()"""
-        resize = _testcapi.bytes_resize
+        _resize = _testcapi.bytes_resize
+
+        def resize(obj, size, new):
+            result = _resize(obj, size, new)
+            if 1 <= len(result):
+                if new or size != len(obj):
+                    # gh-156995: Make sure that the result is a fresh object.
+                    # Previously, _PyBytes_Resize(&obj, 1) returned a singleton
+                    # if _PyObject_IsUniquelyReferenced() is false.
+                    self.assertEqual(sys.getrefcount(result), 1)
+                    self.assertFalse(sys._is_immortal(result))
+            else:
+                # check that the result is the empty bytes string singleton
+                self.assertTrue(sys._is_immortal(result))
+            return result
 
         for new in True, False:
-            self.assertEqual(resize(b'abc', 0, new), b'')
-            self.assertEqual(resize(b'abc', 1, new), b'a')
-            self.assertEqual(resize(b'abc', 2, new), b'ab')
-            self.assertEqual(resize(b'abc', 3, new), b'abc')
-            b = resize(b'abc', 4, new)
-            self.assertEqual(len(b), 4)
-            self.assertEqual(b[:3], b'abc')
-
-            self.assertEqual(resize(b'a', 0, new), b'')
-            self.assertEqual(resize(b'a', 1, new), b'a')
-            b = resize(b'a', 2, new)
-            self.assertEqual(len(b), 2)
-            self.assertEqual(b[:1], b'a')
-
-            self.assertEqual(resize(b'', 0, new), b'')
-            self.assertEqual(len(resize(b'', 1, new)), 1)
-            self.assertEqual(len(resize(b'', 2, new)), 2)
+            with self.subTest(new=new):
+                self.assertEqual(resize(b'abc', 0, new), b'')
+                self.assertEqual(resize(b'abc', 1, new), b'a')
+                self.assertEqual(resize(b'abc', 2, new), b'ab')
+                self.assertEqual(resize(b'abc', 3, new), b'abc')
+                b = resize(b'abc', 4, new)
+                self.assertEqual(len(b), 4)
+                self.assertEqual(b[:3], b'abc')
+
+                self.assertEqual(resize(b'a', 0, new), b'')
+                self.assertEqual(resize(b'a', 1, new), b'a')
+                b = resize(b'a', 2, new)
+                self.assertEqual(len(b), 2)
+                self.assertEqual(b[:1], b'a')
+
+                self.assertEqual(resize(b'', 0, new), b'')
+                self.assertEqual(len(resize(b'', 1, new)), 1)
+                self.assertEqual(len(resize(b'', 2, new)), 2)
 
         self.assertRaises(SystemError, resize, b'abc', -1, False)
         self.assertRaises(SystemError, resize, bytearray(b'abc'), 3, False)

Comment thread Objects/bytesobject.c
Co-authored-by: Victor Stinner <vstinner@python.org>

vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM. Nice fix. Modified code is well tested by new tests.

vstinner merged commit fd569ea into python:main Sep 10, 2026
58 checks passed

Copy link
Copy Markdown

Thanks @StanFromIreland for the PR, and @vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.15.
🐍🍒⛏🤖

bedevere-app Bot commented Sep 10, 2026

Copy link
Copy Markdown

GH-157260 is a backport of this pull request to the 3.15 branch.

bedevere-app Bot removed the needs backport to 3.15 pre-release feature fixes, bugs and security fixes label Sep 10, 2026
hugovk pushed a commit that referenced this pull request Sep 10, 2026
…le-byte singletons (GH-156996) (#157260)

Co-authored-by: Stan Ulbrych <stan@python.org>
Co-authored-by: Cody Maloney <cmaloney@users.noreply.github.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
StanFromIreland deleted the bytearray.take_bytes branch September 10, 2026 15:30
clin1234 pushed a commit to clin1234/cpython that referenced this pull request Sep 12, 2026
…e-byte singletons (python#156996)

Co-authored-by: Cody Maloney <cmaloney@users.noreply.github.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
vstinner added a commit to vstinner/cpython that referenced this pull request Sep 12, 2026
On Free Threading, _PyBytes_Resize(&obj, 1) no longer returns a
single byte singleton if the current thread is different than the
thread which created the object.

Co-authored-by: Cody Maloney <cmaloney@users.noreply.github.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
(cherry picked from commit fd569ea)

bedevere-app Bot commented Sep 12, 2026

Copy link
Copy Markdown

GH-157373 is a backport of this pull request to the 3.14 branch.

vstinner added a commit that referenced this pull request Sep 13, 2026
…157373)

On Free Threading, _PyBytes_Resize(&obj, 1) no longer returns a
single byte singleton if the current thread is different than the
thread which created the object.

Co-authored-by: Cody Maloney <cmaloney@users.noreply.github.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
(cherry picked from commit fd569ea)

Co-authored-by: Stan Ulbrych <stan@python.org>
Co-authored-by: Cody Maloney <cmaloney@users.noreply.github.com>
vstinner added a commit to vstinner/cpython that referenced this pull request Sep 13, 2026
…on#156996) (python#157373)

_PyBytes_Resize(&obj, 1) no longer returns a single byte singleton if
the reference count is greater than 1.

(cherry picked from commit fd569ea)

Co-authored-by: Stan Ulbrych <stan@python.org>
Co-authored-by: Cody Maloney <cmaloney@users.noreply.github.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
vstinner added a commit that referenced this pull request Sep 13, 2026
…157373) (#157436)

[3.14] gh-156995: Fix _PyBytes_Resize() in Free Threading (#156996) (#157373)

_PyBytes_Resize(&obj, 1) no longer returns a single byte singleton if
the reference count is greater than 1.

(cherry picked from commit fd569ea)

Co-authored-by: Stan Ulbrych <stan@python.org>
Co-authored-by: Cody Maloney <cmaloney@users.noreply.github.com>
johnslavik pushed a commit to johnslavik/cpython that referenced this pull request Sep 14, 2026
…e-byte singletons (python#156996)

Co-authored-by: Cody Maloney <cmaloney@users.noreply.github.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants


Back | FazBrowse Home | New Git URL