Skip to content

Commit 70b2fca

Browse files
committed
Fix #76778: array_reduce leaks memory if callback throws exception
We have to release the result variable in the error case, too.
1 parent cf2fc66 commit 70b2fca

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ PHP NEWS
4141
. Fixed bug #68175 (RegexIterator pregFlags are NULL instead of 0). (Tim
4242
Siebels)
4343

44+
- Standard:
45+
. Fixed bug #76778 (array_reduce leaks memory if callback throws exception).
46+
(cmb)
47+
4448
- zlib:
4549
. Fixed bug #65988 (Zlib version check fails when an include/zlib/ style dir
4650
is passed to the --with-zlib configure option). (Jay Bonci)

ext/standard/array.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5243,6 +5243,7 @@ PHP_FUNCTION(array_reduce)
52435243
} else {
52445244
zval_ptr_dtor(&args[1]);
52455245
zval_ptr_dtor(&args[0]);
5246+
zval_ptr_dtor(&result);
52465247
return;
52475248
}
52485249
} ZEND_HASH_FOREACH_END();
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
Bug #76778 (array_reduce leaks memory if callback throws exception)
3+
--SKIPIF--
4+
<?php
5+
if (getenv("SKIP_SLOW_TESTS")) die('skip slow test');
6+
?>
7+
--INI--
8+
memory_limit=32M
9+
--FILE--
10+
<?php
11+
for ($i = 0; $i < 100; $i++) {
12+
try {
13+
array_reduce(
14+
[1],
15+
function ($carry, $item) {
16+
throw new Exception;
17+
},
18+
range(1, 200000)
19+
);
20+
} catch (Exception $e) {
21+
}
22+
}
23+
?>
24+
===DONE===
25+
--EXPECT--
26+
===DONE===

0 commit comments

Comments
 (0)