Skip to content

Commit 5406176

Browse files
committed
Merge branch 'PHP-8.4' into PHP-8.5
* PHP-8.4: ext/pcre: fix memory leaks on error paths
2 parents 908723a + 78702fa commit 5406176

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

ext/pcre/php_pcre.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,7 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, bo
831831
if (key != regex) {
832832
zend_string_release_ex(key, 0);
833833
}
834+
pcre2_code_free(new_entry.re);
834835
php_error_docref(NULL, E_WARNING, "Internal pcre2_pattern_info() error %d", rc);
835836
pcre_handle_exec_error(PCRE2_ERROR_INTERNAL);
836837
return NULL;
@@ -841,6 +842,7 @@ PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, bo
841842
if (key != regex) {
842843
zend_string_release_ex(key, 0);
843844
}
845+
pcre2_code_free(new_entry.re);
844846
php_error_docref(NULL, E_WARNING, "Internal pcre_pattern_info() error %d", rc);
845847
pcre_handle_exec_error(PCRE2_ERROR_INTERNAL);
846848
return NULL;
@@ -1286,7 +1288,18 @@ PHPAPI void php_pcre_match_impl(pcre_cache_entry *pce, zend_string *subject_str,
12861288
if (subpats != NULL) {
12871289
/* Try to get the list of substrings and display a warning if failed. */
12881290
if (UNEXPECTED(offsets[1] < offsets[0])) {
1289-
if (match_sets) efree(match_sets);
1291+
if (match_sets) {
1292+
for (i = 0; i < num_subpats; i++) {
1293+
zend_array_destroy(match_sets[i]);
1294+
}
1295+
efree(match_sets);
1296+
}
1297+
if (marks) {
1298+
zend_array_destroy(marks);
1299+
}
1300+
if (match_data != mdata) {
1301+
pcre2_match_data_free(match_data);
1302+
}
12901303
php_error_docref(NULL, E_WARNING, "Get subpatterns list failed");
12911304
RETURN_FALSE;
12921305
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
preg_match_all() resource cleanup when \K in lookahead causes negative-length match
3+
--FILE--
4+
<?php
5+
$result = preg_match_all('/(?=ab\K)a/', 'ab', $matches);
6+
var_dump($result);
7+
?>
8+
--EXPECTF--
9+
Warning: preg_match_all(): Get subpatterns list failed in %s on line %d
10+
bool(false)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
preg_match() resource cleanup when \K in lookahead causes negative-length match
3+
--FILE--
4+
<?php
5+
$result = preg_match('/(?=ab\K)a/', 'ab', $matches);
6+
var_dump($result);
7+
?>
8+
--EXPECTF--
9+
Warning: preg_match(): Get subpatterns list failed in %s on line %d
10+
bool(false)

0 commit comments

Comments
 (0)