Skip to content

[3.11] gh-98783: Fix crashes when str subclasses are used in _PyUnicode_Equal (GH-98806) #98869

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
2 changes: 1 addition & 1 deletion Include/cpython/unicodeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -1144,7 +1144,7 @@ PyAPI_FUNC(PyObject*) _PyUnicode_FromId(_Py_Identifier*);
and where the hash values are equal (i.e. a very probable match) */
PyAPI_FUNC(int) _PyUnicode_EQ(PyObject *, PyObject *);

/* Equality check. Returns -1 on failure. */
/* Equality check. */
Copy link
Member

Choose a reason for hiding this comment

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

Actually, in 3.11, this can return -1 on failure.

Copy link
Member

Choose a reason for hiding this comment

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

I can backport manually without this comment change.

Copy link
Member

Choose a reason for hiding this comment

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

PyAPI_FUNC(int) _PyUnicode_Equal(PyObject *, PyObject *);

PyAPI_FUNC(int) _PyUnicode_WideCharString_Converter(PyObject *, void *);
Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_descr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,15 @@ class X(object):
with self.assertRaisesRegex(AttributeError, "'X' object has no attribute 'a'"):
X().a

# Test string subclass in `__slots__`, see gh-98783
class SubStr(str):
pass
class X(object):
__slots__ = (SubStr('x'),)
X().x = 1
with self.assertRaisesRegex(AttributeError, "'X' object has no attribute 'a'"):
X().a

def test_slots_special(self):
# Testing __dict__ and __weakref__ in __slots__...
class D(object):
Expand Down Expand Up @@ -3581,6 +3590,16 @@ def __repr__(self):
self.assertEqual(o.__str__(), '41')
self.assertEqual(o.__repr__(), 'A repr')

def test_repr_with_module_str_subclass(self):
# gh-98783
class StrSub(str):
pass
class Some:
pass
Some.__module__ = StrSub('example')
self.assertIsInstance(repr(Some), str) # should not crash
self.assertIsInstance(repr(Some()), str) # should not crash

def test_keyword_arguments(self):
# Testing keyword arguments to __init__, __call__...
def f(a): return a
Expand Down
12 changes: 12 additions & 0 deletions Lib/test/test_long.py
Original file line number Diff line number Diff line change
Expand Up @@ -1334,6 +1334,12 @@ def equivalent_python(n, length, byteorder, signed=False):
b'\xff\xff\xff\xff\xff')
self.assertRaises(OverflowError, (1).to_bytes, 0, 'big')

# gh-98783
class SubStr(str):
pass
self.assertEqual((0).to_bytes(1, SubStr('big')), b'\x00')
self.assertEqual((0).to_bytes(0, SubStr('little')), b'')

def test_from_bytes(self):
def check(tests, byteorder, signed=False):
def equivalent_python(byte_array, byteorder, signed=False):
Expand Down Expand Up @@ -1534,6 +1540,12 @@ def __bytes__(self):
self.assertRaises(TypeError, int.from_bytes, MissingBytes())
self.assertRaises(ZeroDivisionError, int.from_bytes, RaisingBytes())

# gh-98783
class SubStr(str):
pass
self.assertEqual(int.from_bytes(b'', SubStr('big')), 0)
self.assertEqual(int.from_bytes(b'\x00', SubStr('little')), 0)

@support.cpython_only
def test_from_bytes_small(self):
# bpo-46361
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix multiple crashes in debug mode when ``str`` subclasses
are used instead of ``str`` itself.
4 changes: 2 additions & 2 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -11134,8 +11134,8 @@ unicode_compare_eq(PyObject *str1, PyObject *str2)
int
_PyUnicode_Equal(PyObject *str1, PyObject *str2)
{
assert(PyUnicode_CheckExact(str1));
assert(PyUnicode_CheckExact(str2));
assert(PyUnicode_Check(str1));
assert(PyUnicode_Check(str2));
if (str1 == str2) {
return 1;
}
Expand Down