]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] gh-140797: Forbid capturing groups in re.Scanner lexicon patterns (GH-140944...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Tue, 4 Nov 2025 11:17:29 +0000 (12:17 +0100)
committerGitHub <noreply@github.com>
Tue, 4 Nov 2025 11:17:29 +0000 (11:17 +0000)
(cherry picked from commit fa9c3eefd475f0647a69bf3f49db8100848fb6a9)

Co-authored-by: Abhishek Tiwari <Abhi210@users.noreply.github.com>
Lib/re/__init__.py
Lib/test/test_re.py
Misc/NEWS.d/next/Library/2025-11-03-16-23-54.gh-issue-140797.DuFEeR.rst [new file with mode: 0644]

index 7e8abbf6ffe15526495a4241ed2153d34a1beba9..5a821411ad6e509ca6aeded7967feb5a6072d0dd 100644 (file)
@@ -399,9 +399,12 @@ class Scanner:
         s = _parser.State()
         s.flags = flags
         for phrase, action in lexicon:
+            sub_pattern = _parser.parse(phrase, flags)
+            if sub_pattern.state.groups != 1:
+                raise ValueError("Cannot use capturing groups in re.Scanner")
             gid = s.opengroup()
             p.append(_parser.SubPattern(s, [
-                (SUBPATTERN, (gid, 0, 0, _parser.parse(phrase, flags))),
+                (SUBPATTERN, (gid, 0, 0, sub_pattern)),
                 ]))
             s.closegroup(gid, p[-1])
         p = _parser.SubPattern(s, [(BRANCH, (None, p))])
index 813cb4a3f54f2311ecdfbceb3accf688dd6dc063..8d39e7f208666632cfa660b704bfa572a0b413d9 100644 (file)
@@ -1638,6 +1638,24 @@ class ReTests(unittest.TestCase):
                          (['sum', 'op=', 3, 'op*', 'foo', 'op+', 312.5,
                            'op+', 'bar'], ''))
 
+    def test_bug_gh140797(self):
+        # gh140797: Capturing groups are not allowed in re.Scanner
+
+        msg = r"Cannot use capturing groups in re\.Scanner"
+        # Capturing group throws an error
+        with self.assertRaisesRegex(ValueError, msg):
+            Scanner([("(a)b", None)])
+
+        # Named Group
+        with self.assertRaisesRegex(ValueError, msg):
+            Scanner([("(?P<name>a)", None)])
+
+        # Non-capturing groups should pass normally
+        s = Scanner([("(?:a)b", lambda scanner, token: token)])
+        result, rem = s.scan("ab")
+        self.assertEqual(result,['ab'])
+        self.assertEqual(rem,'')
+
     def test_bug_448951(self):
         # bug 448951 (similar to 429357, but with single char match)
         # (Also test greedy matches.)
diff --git a/Misc/NEWS.d/next/Library/2025-11-03-16-23-54.gh-issue-140797.DuFEeR.rst b/Misc/NEWS.d/next/Library/2025-11-03-16-23-54.gh-issue-140797.DuFEeR.rst
new file mode 100644 (file)
index 0000000..493b740
--- /dev/null
@@ -0,0 +1,2 @@
+The undocumented :class:`!re.Scanner` class now forbids regular expressions containing capturing groups in its lexicon patterns. Patterns using capturing groups could
+previously lead to crashes with segmentation fault. Use non-capturing groups (?:...) instead.