Skip to content

Conversation

@paolopas
Copy link
Contributor

@paolopas paolopas commented Jan 4, 2026

This PR fixes two bugs in regexps, but more importantly, it prevents regexp errors from always ending up in a core dump.

  • be27e58 enhanced type safety on regexp implementation
  • 6e61515 fixed memory leak in b2::regex::compiler::regcomp
  • 3394de1 fixed crashing of bad regexps, due to attempt to reference a compiled regexp, which has not been compiled because of some error, by b2::regex::program::result_iterator::advance
  • 0ea95be added the b2::regex::frame variable for formatting errors when using regexps (especially compilation) and program termination (optional).

Be careful, now that b2 no longer core dumps when regexps contain errors, you have to consider that the errors could (and certainly will) go undetected.
Since these are rather subtle errors, it's not a good idea to ignore them. For this reason, the default MATCH now terminates execution with a nice error message, similar to the one emitted when the arguments are incorrect, in the case of problems compiling the regexps.
This is actually implemented with a thread_local variable in the b2::regex namespace and a frame_ctx helper class, which is certainly not the cleanest way to do it but currently allows you to customize the error behavior with a minimal impact.
A grep 'regex::program' * in the sources allows you to quickly find where you are using regexps and you may want to force error checking, for example in the constructor of regex_grep_task (although you need to know the Frame*).

  • 1795de2 b2::regex::program::result_iterator::operator[] bug on boundary check fixed
  • 356ff24 added b2::regex::program::result_iterator::count method, for better regex usability
  • c1341c7 better cleanup on abs_workdir.py test

With these changes, MATCH not only no longer crashes on every error in a regex, but it also reports a detailed error to the user and stops execution. Example #488

MATCH \\"f...o\\" : furbo ;

now behaves like this:

# b2
Jamroot:1: in modules.load
*** regexp error
* rule MATCH called with: ( \f...o\ : furbo )
* trailing \
(builtin):/home/pax/.local/share/b2/src/build/project.jam:586: in load-jamfile
/home/pax/.local/share/b2/src/build/project.jam:84: in load
/home/pax/.local/share/b2/src/build/project.jam:241: in project.find
/home/pax/.local/share/b2/src/build-system.jam:639: in module scope

This partially fixes #488, but it isn't all.
René, as far as I'm concerned, you can close this PR, since I'll need some time to check all the regexes out there and write a test that has good coverage.

due to attempt to reference a compiled regexp,
which has not been compiled due to some error,
by b2::regex::program::result_iterator::advance
for formatting errors when using regexps (especially compilation)
and program termination (optional).
method, for better regex usability
@grafikrobot
Copy link
Member

Obsoleted by #490

@grafikrobot grafikrobot closed this Jan 7, 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.

Weird regexps

2 participants