Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
17 changes: 17 additions & 0 deletions Lib/test/test_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,23 @@ def test_replace_id(self):
text = 'abc def'
self.assertIs(text.replace(pattern, pattern), text)

@support.cpython_only
@unittest.skipIf(_testcapi is None, reason="_testcapi is required for this test")
Comment thread
sobolevn marked this conversation as resolved.
@unittest.skipIf(support.Py_TRACE_REFS, 'cannot test Py_TRACE_REFS build')
def test_replace_oom(self):
# https://github.com/python/cpython/issues/152228
s1 = "轘" * 4
s2 = "&"
s3 = "&"
assertion = self.assertRaises(MemoryError)
_testcapi.set_nomemory(0, 0)
try:
# No allocations made in the test itself:
with assertion:
s1.replace(s2, s3) # this line used to crash before
finally:
_testcapi.remove_mem_hooks()

def test_repeat_id_preserving(self):
a = '123abc1@'
b = '456zyx-+'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a crash in :meth:`str.replace` under a limited memory situation.
18 changes: 9 additions & 9 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -10753,9 +10753,9 @@ replace(PyObject *self, PyObject *str1,
}

done:
assert(srelease == (sbuf != PyUnicode_DATA(self)));
assert(release1 == (buf1 != PyUnicode_DATA(str1)));
assert(release2 == (buf2 != PyUnicode_DATA(str2)));
assert(srelease == (sbuf != NULL && sbuf != PyUnicode_DATA(self)));
assert(release1 == (buf1 != NULL && buf1 != PyUnicode_DATA(str1)));
assert(release2 == (buf2 != NULL && buf2 != PyUnicode_DATA(str2)));
if (srelease)
PyMem_Free((void *)sbuf);
if (release1)
Expand All @@ -10767,9 +10767,9 @@ replace(PyObject *self, PyObject *str1,

nothing:
/* nothing to replace; return original string (when possible) */
assert(srelease == (sbuf != PyUnicode_DATA(self)));
assert(release1 == (buf1 != PyUnicode_DATA(str1)));
assert(release2 == (buf2 != PyUnicode_DATA(str2)));
assert(srelease == (sbuf != NULL && sbuf != PyUnicode_DATA(self)));
assert(release1 == (buf1 != NULL && buf1 != PyUnicode_DATA(str1)));
assert(release2 == (buf2 != NULL && buf2 != PyUnicode_DATA(str2)));
if (srelease)
PyMem_Free((void *)sbuf);
if (release1)
Expand All @@ -10779,9 +10779,9 @@ replace(PyObject *self, PyObject *str1,
return unicode_result_unchanged(self);

error:
assert(srelease == (sbuf != PyUnicode_DATA(self)));
assert(release1 == (buf1 != PyUnicode_DATA(str1)));
assert(release2 == (buf2 != PyUnicode_DATA(str2)));
assert(srelease == (sbuf != NULL && sbuf != PyUnicode_DATA(self)));
assert(release1 == (buf1 != NULL && buf1 != PyUnicode_DATA(str1)));
assert(release2 == (buf2 != NULL && buf2 != PyUnicode_DATA(str2)));
if (srelease)
PyMem_Free((void *)sbuf);
if (release1)
Expand Down
Loading