]> git.ipfire.org Git - thirdparty/Python/cpython.git/commit
gh-150717: Avoid mark-array allocation for groupless regex patterns (GH-150719)
authorBernát Gábor <gaborjbernat@gmail.com>
Tue, 2 Jun 2026 07:45:30 +0000 (00:45 -0700)
committerGitHub <noreply@github.com>
Tue, 2 Jun 2026 07:45:30 +0000 (10:45 +0300)
commitc79e18a8e5ff4fda1a3d9201e65b0c6048b56b68
tree7a74c68d6a2aa5711efd2b1f99fa917c2f24404d
parent27ebd9abce76543e95748a7534e5218acd514db2
gh-150717: Avoid mark-array allocation for groupless regex patterns (GH-150719)

state_init() always did PyMem_New(state->mark, groups*2), which for a
pattern with no capturing groups is PyMem_Malloc(0) -- a real allocation
(plus matching free) on every match/search/fullmatch call, for an array
that is never read: groupless patterns emit no MARK opcodes and group 0's
span is taken from state->start/ptr.

Guard the allocation with `if (pattern->groups)`. state->mark stays NULL
(set by the preceding memset), and both the error path and state_fini
already PyMem_Free(NULL) safely.
Misc/NEWS.d/next/Library/2026-06-01-08-12-34.gh-issue-150717.LVRJXH.rst [new file with mode: 0644]
Modules/_sre/sre.c