Skip to content

fix: register the regex pool cleanup right after a successful compile - #3673

Open
fzipi wants to merge 1 commit into
owasp-modsecurity:v2/masterfrom
fzipi:v2/issue-2407-pcre-leak
Open

fzipi wants to merge 1 commit into
owasp-modsecurity:v2/masterfrom
fzipi:v2/issue-2407-pcre-leak

Conversation

@fzipi

@fzipi fzipi commented Oct 13, 2026

Copy link
Copy Markdown
Collaborator

What

msc_pregcomp_ex (apache2/msc_pcre.c) registers the msc_pcre_cleanup pool cleanup right after the pattern compiles successfully, in both the PCRE2 and the legacy PCRE branch, instead of only at the end of the function.

Why

Every failure path between the compile and the return (pcre2_match_context_create with PCRE2, the pcre_extra allocation with legacy PCRE) returned NULL without freeing the compiled pattern, as reported in #2407. With the cleanup registered as soon as there is something to release, those paths are covered; msc_pcre_cleanup already tolerates a NULL match context / pcre_extra.

Note that in the current code pcre2_jit_compile failing does not return early (the result is kept in jit_compile_rc), so the window is the allocation failures only. The reordering costs nothing and removes it.

Tests

No new test: the leak only shows on an allocation failure. Verified in a Debian bookworm container (apache2 2.4, PCRE2 with JIT) that the branch builds and that the unit tests and tests/regression/misc/10-pcre.t pass as on untouched v2/master (the one unit-test failure, an ipMatch assertion with --enable-assertions, is pre-existing on v2/master).

References

Fixes #2407

🤖 Generated with Claude Code

msc_pregcomp_ex registered msc_pcre_cleanup only at the very end, so on
the failure paths between the compile and the return (the match context
allocation with PCRE2, the pcre_extra allocation with legacy PCRE) the
function returned NULL and the compiled pattern was never freed.

The cleanup is now registered as soon as the compile succeeds; it
already tolerates a NULL match context / pcre_extra.

Fixes owasp-modsecurity#2407

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0181jBv5JdNQviwDzfnkf1Gy
@coderabbitai

coderabbitai Bot commented Oct 13, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 6863ef48-5e3a-4684-a1f6-256f600e3bc9

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@sonarqubecloud

Copy link
Copy Markdown

This branch has not been deployed

No deployments
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