8000 [3.11] gh-110590: Fix a bug where _sre.compile would overwrite except… · python/cpython@7fefed0 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7fefed0

Browse files
[3.11] gh-110590: Fix a bug where _sre.compile would overwrite exceptions (GH-110591) (#110614)
TypeError would be overwritten by OverflowError if 'code' param contained non-ints. (cherry picked from commit 344d3a2) Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
1 parent 1942721 commit 7fefed0

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-0
lines changed

Lib/test/test_re.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2725,6 +2725,9 @@ def test_dealloc(self):
27252725
_sre.compile("abc", 0, [long_overflow], 0, {}, ())
27262726
with self.assertRaises(TypeError):
27272727
_sre.compile({}, 0, [], 0, [], [])
2728+
# gh-110590: `TypeError` was overwritten with `OverflowError`:
2729+
with self.assertRaises(TypeError):
2730+
_sre.compile('', 0, ['abc'], 0, {}, ())
27282731

27292732
@cpython_only
27302733
def test_repeat_minmax_overflow_maxrepeat(self):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Fix a bug in :meth:`!_sre.compile` where :exc:`TypeError`
2+
would be overwritten by :exc:`OverflowError` when
3+
the *code* argument was a list of non-ints.

Modules/_sre/sre.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1437,6 +1437,9 @@ _sre_compile_impl(PyObject *module, PyObject *pattern, int flags,
14371437
for (i = 0; i < n; i++) {
14381438
PyObject *o = PyList_GET_ITEM(code, i);
14391439
unsigned long value = PyLong_AsUnsignedLong(o);
1440+
if (value == (unsigned long)-1 && PyErr_Occurred()) {
1441+
break;
1442+
}
14401443
self->code[i] = (SRE_CODE) value;
14411444
if ((unsigned long) self->code[i] != value) {
14421445
PyErr_SetString(PyExc_OverflowError,

0 commit comments

Comments
 (0)
0