Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[3.13] gh-142557: fix UAF inbytearray.__mod__ when object is mutated while formatting%-style arguments (GH-143213)#143229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
picnixz merged 1 commit intopython:3.13frompicnixz:backport-61ee048-3.13
Dec 27, 2025
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletionsLib/test/test_bytes.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1349,6 +1349,18 @@ def test_bytearray_api(self):
except OSError:
pass

def test_mod_concurrent_mutation(self):
# Prevent crash in __mod__ when formatting mutates the bytearray.
# Regression test for https://github.com/python/cpython/issues/142557.
fmt = bytearray(b"%a end")

class S:
def __repr__(self):
fmt.clear()
return "E"

self.assertRaises(BufferError, fmt.__mod__, S())

def test_reverse(self):
b = bytearray(b'hello')
self.assertEqual(b.reverse(), None)
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
Fix a use-after-free crash in :ref:`bytearray.__mod__ <bytes-formatting>` when
the :class:`!bytearray` is mutated while formatting the ``%``-style arguments.
Patch by Bénédikt Tran.
10 changes: 9 additions & 1 deletionObjects/bytearrayobject.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2401,7 +2401,15 @@ bytearray_mod(PyObject *v, PyObject *w)
{
if (!PyByteArray_Check(v))
Py_RETURN_NOTIMPLEMENTED;
return _PyBytes_FormatEx(PyByteArray_AS_STRING(v), PyByteArray_GET_SIZE(v), w, 1);

PyByteArrayObject *self = _PyByteArray_CAST(v);
/* Increase exports to prevent bytearray storage from changing during op. */
self->ob_exports++;
PyObject *res = _PyBytes_FormatEx(
PyByteArray_AS_STRING(v), PyByteArray_GET_SIZE(v), w, 1
);
self->ob_exports--;
return res;
}

static PyNumberMethods bytearray_as_number = {
Expand Down
Loading

[8]ページ先頭

©2009-2026 Movatter.jp