Skip to content

ext/pcre: fix memory leaks on error paths#21298

Closed
devnexen wants to merge 1 commit intophp:PHP-8.4from
devnexen:pcre2_more_mem_leaks
Closed

ext/pcre: fix memory leaks on error paths#21298
devnexen wants to merge 1 commit intophp:PHP-8.4from
devnexen:pcre2_more_mem_leaks

Conversation

@devnexen
Copy link
Member

Fix pcre2_code leak when pcre2_pattern_info() fails after a successful pcre2_compile(), and fix match_sets/match_data/marks leak when offsets[1] < offsets[0] in php_pcre_match_impl().

@devnexen devnexen marked this pull request as ready for review February 25, 2026 20:55
@devnexen devnexen requested a review from ndossche February 25, 2026 21:08
Copy link
Member

@ndossche ndossche left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice find

}

rc = pcre2_pattern_info(re, PCRE2_INFO_NAMECOUNT, &new_entry.name_count);
if (rc < 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a follow-up, would be nice if the this error-handling code branch and the previous one are somehow merged: they are identical.

if (UNEXPECTED(offsets[1] < offsets[0])) {
if (match_sets) efree(match_sets);
if (match_sets) {
for(i = 0; i < num_subpats; i++) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit:

Suggested change
for(i = 0; i < num_subpats; i++) {
for (i = 0; i < num_subpats; i++) {

Fix pcre2_code leak when pcre2_pattern_info() fails after a successful
pcre2_compile(), and fix match_sets/match_data/marks leak when
offsets[1] < offsets[0] in php_pcre_match_impl().
@devnexen devnexen force-pushed the pcre2_more_mem_leaks branch from 5cb744b to 713f662 Compare February 26, 2026 18:33
@devnexen devnexen closed this in 78702fa Feb 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants