mirror of
https://github.com/python/cpython.git
synced 2025-08-16 14:50:43 +00:00
[3.12] gh-110590: Fix a bug where _sre.compile would overwrite exceptions (GH-110591) (#110613)
TypeError would be overwritten by OverflowError
if 'code' param contained non-ints.
(cherry picked from commit 344d3a222a
)
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
This commit is contained in:
parent
55d607683f
commit
c1e8e90915
3 changed files with 9 additions and 0 deletions
|
@ -2694,6 +2694,9 @@ class ImplementationTest(unittest.TestCase):
|
||||||
_sre.compile("abc", 0, [long_overflow], 0, {}, ())
|
_sre.compile("abc", 0, [long_overflow], 0, {}, ())
|
||||||
with self.assertRaises(TypeError):
|
with self.assertRaises(TypeError):
|
||||||
_sre.compile({}, 0, [], 0, [], [])
|
_sre.compile({}, 0, [], 0, [], [])
|
||||||
|
# gh-110590: `TypeError` was overwritten with `OverflowError`:
|
||||||
|
with self.assertRaises(TypeError):
|
||||||
|
_sre.compile('', 0, ['abc'], 0, {}, ())
|
||||||
|
|
||||||
@cpython_only
|
@cpython_only
|
||||||
def test_repeat_minmax_overflow_maxrepeat(self):
|
def test_repeat_minmax_overflow_maxrepeat(self):
|
||||||
|
|
|
@ -0,0 +1,3 @@
|
||||||
|
Fix a bug in :meth:`!_sre.compile` where :exc:`TypeError`
|
||||||
|
would be overwritten by :exc:`OverflowError` when
|
||||||
|
the *code* argument was a list of non-ints.
|
|
@ -1462,6 +1462,9 @@ _sre_compile_impl(PyObject *module, PyObject *pattern, int flags,
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
PyObject *o = PyList_GET_ITEM(code, i);
|
PyObject *o = PyList_GET_ITEM(code, i);
|
||||||
unsigned long value = PyLong_AsUnsignedLong(o);
|
unsigned long value = PyLong_AsUnsignedLong(o);
|
||||||
|
if (value == (unsigned long)-1 && PyErr_Occurred()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
self->code[i] = (SRE_CODE) value;
|
self->code[i] = (SRE_CODE) value;
|
||||||
if ((unsigned long) self->code[i] != value) {
|
if ((unsigned long) self->code[i] != value) {
|
||||||
PyErr_SetString(PyExc_OverflowError,
|
PyErr_SetString(PyExc_OverflowError,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue