Skip to content

Avoid overlapping regex matches in _key_val_list_pat (Fixes #741) - #742

Open
dirkmueller wants to merge 1 commit into
trentm:masterfrom
dirkmueller:fixup_redos
Open

dirkmueller wants to merge 1 commit into
trentm:masterfrom
dirkmueller:fixup_redos

Conversation

@dirkmueller

@dirkmueller dirkmueller commented Oct 16, 2026 •

Copy link
Copy Markdown

Fixes issue #741

Constrained the metadata key definition to r'[ \t][^\s:\r\n][^:\r\n]', ensuring keys cannot match colons or newlines and cannot backtrack across key/value delimiters.
Kept simple single-line values bounded by [^\n]\n?. Simplified to r"^-[ \t]([^\n]*)(?:\n((?:[ \t]+[^\n]+\n?)+))?", removing the overlapping space quantifiers before [:-]. Updated parse_structured_value to support both 2-tuple and 3-tuple matches for full backward compatibility.

Added tests

Verification:

┌────────────┬────────────┬───────────┐
│ Input Size │ Before Fix │ After Fix │
├────────────┼────────────┼───────────┤
│ 8 KB │ ~0.4 s │ ~0.003 s │
│ 16 KB │ ~1.6 s │ ~0.0003 s │
│ 32 KB │ ~6.2 s │ ~0.0005 s │
│ 64 KB │ ~24.0 s │ ~0.0008 s │
│ 128 KB │ ~91.0 s │ ~0.0015 s │
└────────────┴────────────┴───────────┘

Comment thread test/test_markdown2.py Outdated
Constrained the metadata key definition to r'[ \t]*[^\s:\r\n][^:\r\n]*', ensuring keys cannot match colons or newlines and cannot backtrack across key/value
delimiters.
Kept simple single-line values bounded by [^\n]*\n?.
Simplified to r"^-[ \t]*([^\n]*)(?:\n((?:[ \t]+[^\n]+\n?)+))?", removing the overlapping space quantifiers before [:-].
Updated parse_structured_value to support both 2-tuple and 3-tuple matches for full backward compatibility.

Added tests

Verification:

  ┌────────────┬────────────┬───────────┐
  │ Input Size │ Before Fix │ After Fix │
  ├────────────┼────────────┼───────────┤
  │ 8 KB       │ ~0.4 s     │ ~0.003 s  │
  │ 16 KB      │ ~1.6 s     │ ~0.0003 s │
  │ 32 KB      │ ~6.2 s     │ ~0.0005 s │
  │ 64 KB      │ ~24.0 s    │ ~0.0008 s │
  │ 128 KB     │ ~91.0 s    │ ~0.0015 s │
  └────────────┴────────────┴───────────┘
@Crozzers

Copy link
Copy Markdown
Contributor

Can't see any issues, and tests pass locally

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants