Skip to content

Commit 6c45f7a

Browse files
committed
ext/pcre: preg_match() fix memory leak with invalid regexes.
close GH-21290
1 parent ec5a1e0 commit 6c45f7a

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ PHP NEWS
5555
. Fixed pcntl_signal_dispatch() stale pointer and exception
5656
handling. (David Carlier)
5757

58+
- PCRE:
59+
. Fixed preg_match memory leak with invalid regexes. (David Carlier)
60+
5861
- PDO_PGSQL:
5962
. Fixed bug GH-21055 (connection attribute status typo for GSS negotiation).
6063
(lsaos)

ext/pcre/php_pcre.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1489,7 +1489,8 @@ ZEND_FRAMELESS_FUNCTION(preg_match, 2)
14891489
/* Compile regex or get it from cache. */
14901490
pcre_cache_entry *pce;
14911491
if ((pce = pcre_get_compiled_regex_cache(regex)) == NULL) {
1492-
RETURN_FALSE;
1492+
RETVAL_FALSE;
1493+
goto flf_clean;
14931494
}
14941495

14951496
pce->refcount++;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
Memory leak in preg_match() frameless function with invalid regex and object arguments
3+
--FILE--
4+
<?php
5+
class Str {
6+
private $val;
7+
public function __construct($val) {
8+
$this->val = $val;
9+
}
10+
public function __toString() {
11+
return $this->val;
12+
}
13+
}
14+
15+
$regex = new Str("invalid regex");
16+
$subject = new Str("some subject");
17+
18+
// Running in a loop to ensure leak detection if run with memory tools
19+
for ($i = 0; $i < 100; $i++) {
20+
@preg_match($regex, $subject);
21+
}
22+
23+
echo "Done";
24+
?>
25+
--EXPECT--
26+
Done

0 commit comments

Comments
 (0)