KEMBAR78
`<regex>`: Handle overflow and allocation failure when parsing repetitions by muellerj2 · Pull Request #5716 · microsoft/STL · GitHub
Skip to content

Conversation

@muellerj2
Copy link
Contributor

@muellerj2 muellerj2 commented Sep 12, 2025

This fixes two obscure bugs when parsing repetitions:

  • The _Loops counter can potentially overflow, which will result in a crash during matching. The fix is to throw a regex_error (of suitable type error_space) when overflow is about to occur during parsing.
  • The _Node_end_rep object can potentially be leaked if the subsequent new _Node_rep(...) throws an exception. The simplest fix is to link the _Node_end_rep object into the NFA before allocating and constructing the _Node_rep object, so the _Node_end_rep object will be cleaned up together with the rest of the NFA by the _Tidy_guard in _Parser2::_Compile if an exception occurs.

No test coverage added because testing these bugs is impractical. In particular, the first bug occurs only after more than 500 GB have been allocated to represent the NFA.

@muellerj2 muellerj2 requested a review from a team as a code owner September 12, 2025 21:34
@github-project-automation github-project-automation bot moved this to Initial Review in STL Code Reviews Sep 12, 2025
@StephanTLavavej StephanTLavavej added bug Something isn't working regex meow is a substring of homeowner labels Sep 13, 2025
@StephanTLavavej StephanTLavavej self-assigned this Sep 13, 2025
@StephanTLavavej StephanTLavavej removed their assignment Sep 17, 2025
@StephanTLavavej
Copy link
Member

Thanks for noticing these subtle problems and clearly explaining them! 😻

@StephanTLavavej StephanTLavavej moved this from Initial Review to Ready To Merge in STL Code Reviews Sep 17, 2025
@StephanTLavavej StephanTLavavej moved this from Ready To Merge to Merging in STL Code Reviews Sep 19, 2025
@StephanTLavavej
Copy link
Member

I'm mirroring this to the MSVC-internal repo - please notify me if any further changes are pushed.

@StephanTLavavej StephanTLavavej merged commit 5165af0 into microsoft:main Sep 22, 2025
39 checks passed
@github-project-automation github-project-automation bot moved this from Merging to Done in STL Code Reviews Sep 22, 2025
@StephanTLavavej
Copy link
Member

Thanks for noticing and fixing these corner cases! 🦅 👁️ 🛠️

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

Labels

bug Something isn't working regex meow is a substring of homeowner

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants