Skip to content

🏗️🔧:read a commit message in linear time - #1821

Merged
openinf-commit-queue[bot] merged 1 commit into
livefrom
fix/no-quadratic-regexes
Aug 15, 2026
Merged

🏗️🔧:read a commit message in linear time#1821
openinf-commit-queue[bot] merged 1 commit into
livefrom
fix/no-quadratic-regexes

Conversation

@DerekNonGeneric

Copy link
Copy Markdown
Member

From the CodeQL alert on #1814. Unlike the semgrep one, this is a true positive, and the input really is uncontrolled: a commit message comes from whoever opened the pull request, and the commit queue reads it while holding credentials that can write to this repository.

Measured before fixing

 10000 newlines + 'x':    57.9 ms
 20000 newlines + 'x':   233.2 ms
 40000 newlines + 'x':   931.1 ms
 80000 newlines + 'x':  3728.9 ms

Four times the work for twice the input. A million newlines — trivial to put in a commit message — would hang the queue for minutes.

A second one CodeQL did not report

Sweeping every regex I had written turned up another, and worse:

20k input
/[\r\n]+$/ 233 ms the reported one
/^\S+:\S+( \S+)*$/ 1506 ms not reported
/\s#\d+$/ 0 ms fine
/^_?[a-z0-9]+(?:-[a-z0-9]+)*$/ 0 ms fine
/^[A-Z][A-Z0-9_-]*$/ 0 ms fine

\S matches a colon, so \S+:\S+ let the engine try every colon in an Assisted-by value as the split point. /^[^\s:]+:\S+( \S+)*$/ gives identical verdicts on every real and rejected value, and runs 8000 times faster.

And a third of the time was elsewhere

countGraphemes built a new Intl.Segmenter per call — once per line of the message. Hoisting it to one instance cut 200,000 calls from 1174 ms to 294 ms. The three call sites for stripping trailing newlines are now one shared linesOf, written as a scan.

After: 50k → 104 ms, 100k → 243 ms, 200k → 507 ms. Linear.

The guards

Two tests with a 3-second budget, on input where the old spellings are unmistakable. Verified by putting each one back:

reverted test result
/[\r\n]+$/ fails after 47.4 s
/^\S+:\S+( \S+)*$/ fails after 40.9 s

Verification

  • nps test — 16/16 tasks, 65/65 tests
  • Every existing test still passes, so behaviour is unchanged; only the time it takes has changed
  • The other three anchored regexes were measured rather than assumed, and left alone

CodeQL was right, and the input really is uncontrolled: a commit message
comes from whoever opened the pull request, and the queue reads it while
holding credentials that can write here. `/[\r\n]+$/` takes time
proportional to the square of the run of newlines it is asked about --
80,000 of them took 3.7 seconds, so a million would hang the job for
minutes.

Sweeping the rest of what I had written found a second one CodeQL did
not report, and worse: `\S` matches a colon, so `^\S+:\S+( \S+)*$` let
the engine try every colon in an Assisted-by value as the split point.
Same verdicts either way, 8000 times faster.

A third of the time was going on building an Intl.Segmenter for every
line rather than once.

Both are held to a budget by tests that fail in forty-odd seconds if
either spelling comes back.

Signed-off-by: Derek Lewis <DerekNonGeneric@inf.is>
Assisted-by: Claude-Code:claude-opus-5
@netlify

netlify Bot commented Aug 15, 2026

Copy link
Copy Markdown

Deploy Preview for gh-pages-openinf ready!

Name Link
🔨 Latest commit 4805f10
🔍 Latest deploy log https://app.netlify.com/projects/gh-pages-openinf/deploys/6a7ff9e07e915a0009e76e20
😎 Deploy Preview https://deploy-preview-1821--gh-pages-openinf.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@DerekNonGeneric DerekNonGeneric added the commit-queue Land this pull request when its checks pass label Aug 15, 2026
@openinf-commit-queue
openinf-commit-queue Bot merged commit fab851e into live Aug 15, 2026
15 checks passed
@openinf-commit-queue openinf-commit-queue Bot removed the commit-queue Land this pull request when its checks pass label Aug 15, 2026
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.

1 participant