]> granicus.if.org Git - python/commitdiff
Issue #29444: Fixed out-of-bounds buffer access in the group() method of
authorSerhiy Storchaka <storchaka@gmail.com>
Sat, 4 Feb 2017 20:53:57 +0000 (22:53 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Sat, 4 Feb 2017 20:53:57 +0000 (22:53 +0200)
the match object.  Based on patch by WGH.

Lib/test/test_re.py
Misc/NEWS
Modules/_sre.c

index 0834fe0f403e91a8b6efbb8ee211f3053bb4eedb..9acd5abbfd77763838315cdc2f536fd65b13b616 100644 (file)
@@ -1679,6 +1679,16 @@ SUBPATTERN None
         self.checkPatternError(r'(?<>)', 'unknown extension ?<>', 1)
         self.checkPatternError(r'(?', 'unexpected end of pattern', 2)
 
+    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):
index 898b1a37298e7adad1cf96e9759122ea92c8b679..55303a51af18b60379267854adea9c0b9d53d94f 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -21,6 +21,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).
 
index 09b58352de4f82d9027c7673dbd4d31f73b0251f..4b376ec078fe1bdbb621b66c48051ecc86b33b6a 100644 (file)
@@ -2015,6 +2015,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 */
@@ -2036,8 +2037,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;