Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7e10dbb

Browse files
Issue python#29444: Fixed out-of-bounds buffer access in the group() method of
the match object. Based on patch by WGH.
1 parent c761136 commit 7e10dbb

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

Lib/test/test_re.py

+10
Original file line numberDiff line numberDiff line change
@@ -1679,6 +1679,16 @@ def test_misc_errors(self):
16791679
self.checkPatternError(r'(?<>)', 'unknown extension ?<>', 1)
16801680
self.checkPatternError(r'(?', 'unexpected end of pattern', 2)
16811681

1682+
def test_bug_29444(self):
1683+
s = bytearray(b'abcdefgh')
1684+
m = re.search(b'[a-h]+', s)
1685+
m2 = re.search(b'[e-h]+', s)
1686+
self.assertEqual(m.group(), b'abcdefgh')
1687+
self.assertEqual(m2.group(), b'efgh')
1688+
s[:] = b'xyz'
1689+
self.assertEqual(m.group(), b'xyz')
1690+
self.assertEqual(m2.group(), b'')
1691+
16821692

16831693
class PatternReprTests(unittest.TestCase):
16841694
def check(self, pattern, expected):

Misc/NEWS

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ Extension Modules
2121
Library
2222
-------
2323

24+
- Issue #29444: Fixed out-of-bounds buffer access in the group() method of
25+
the match object. Based on patch by WGH.
26+
2427
- Issue #29335: Fix subprocess.Popen.wait() when the child process has
2528
exited to a stopped instead of terminated state (ex: when under ptrace).
2629

Modules/_sre.c

+7-2
Original file line numberDiff line numberDiff line change
@@ -2015,6 +2015,7 @@ match_getslice_by_index(MatchObject* self, Py_ssize_t index, PyObject* def)
20152015
Py_buffer view;
20162016
PyObject *result;
20172017
void* ptr;
2018+
Py_ssize_t i, j;
20182019

20192020
if (index < 0 || index >= self->groups) {
20202021
/* raise IndexError if we were given a bad group number */
@@ -2036,8 +2037,12 @@ match_getslice_by_index(MatchObject* self, Py_ssize_t index, PyObject* def)
20362037
ptr = getstring(self->string, &length, &isbytes, &charsize, &view);
20372038
if (ptr == NULL)
20382039
return NULL;
2039-
result = getslice(isbytes, ptr,
2040-
self->string, self->mark[index], self->mark[index+1]);
2040+
2041+
i = self->mark[index];
2042+
j = self->mark[index+1];
2043+
i = Py_MIN(i, length);
2044+
j = Py_MIN(j, length);
2045+
result = getslice(isbytes, ptr, self->string, i, j);
20412046
if (isbytes && view.buf != NULL)
20422047
PyBuffer_Release(&view);
20432048
return result;

0 commit comments

Comments
 (0)