Skip to content

Commit

Permalink
Issue python#29444: Fixed out-of-bounds buffer access in the group() …
Browse files Browse the repository at this point in the history
…method of

the match object.  Based on patch by WGH.
  • Loading branch information
serhiy-storchaka committed Feb 4, 2017
2 parents 75c0d4f + 7e10dbb commit 86e4237
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
10 changes: 10 additions & 0 deletions Lib/test/test_re.py
Original file line number Diff line number Diff line change
Expand Up @@ -1824,6 +1824,16 @@ def test_pattern_compare_bytes(self):
warnings.simplefilter('error', BytesWarning)
self.assertNotEqual(pattern3, pattern1)

def test_bug_29444(self):
s = bytearray(b'abcdefgh')
m = re.search(b'[a-h]+', s)
m2 = re.search(b'[e-h]+', s)
self.assertEqual(m.group(), b'abcdefgh')
self.assertEqual(m2.group(), b'efgh')
s[:] = b'xyz'
self.assertEqual(m.group(), b'xyz')
self.assertEqual(m2.group(), b'')


class PatternReprTests(unittest.TestCase):
def check(self, pattern, expected):
Expand Down
3 changes: 3 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ Extension Modules
Library
-------

- Issue #29444: Fixed out-of-bounds buffer access in the group() method of
the match object. Based on patch by WGH.

- Issue #29335: Fix subprocess.Popen.wait() when the child process has
exited to a stopped instead of terminated state (ex: when under ptrace).

Expand Down
9 changes: 7 additions & 2 deletions Modules/_sre.c
Original file line number Diff line number Diff line change
Expand Up @@ -2003,6 +2003,7 @@ match_getslice_by_index(MatchObject* self, Py_ssize_t index, PyObject* def)
Py_buffer view;
PyObject *result;
void* ptr;
Py_ssize_t i, j;

if (index < 0 || index >= self->groups) {
/* raise IndexError if we were given a bad group number */
Expand All @@ -2024,8 +2025,12 @@ match_getslice_by_index(MatchObject* self, Py_ssize_t index, PyObject* def)
ptr = getstring(self->string, &length, &isbytes, &charsize, &view);
if (ptr == NULL)
return NULL;
result = getslice(isbytes, ptr,
self->string, self->mark[index], self->mark[index+1]);

i = self->mark[index];
j = self->mark[index+1];
i = Py_MIN(i, length);
j = Py_MIN(j, length);
result = getslice(isbytes, ptr, self->string, i, j);
if (isbytes && view.buf != NULL)
PyBuffer_Release(&view);
return result;
Expand Down

0 comments on commit 86e4237

Please sign in to comment.