Skip to content

Commit f322f4a

Browse files
committed
Fix leaks with multiple calls to DatePeriod iterator current()
Destroy the old value first. We can't skip recreating the value because the object may have been changed in between calls.
1 parent 910aeaa commit f322f4a

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

ext/date/php_date.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,6 +1607,7 @@ static zval *date_period_it_current_data(zend_object_iterator *iter)
16071607
php_date_obj *newdateobj;
16081608

16091609
/* Create new object */
1610+
zval_ptr_dtor(&iterator->current);
16101611
php_date_instantiate(get_base_date_class(object->start_ce), &iterator->current);
16111612
newdateobj = Z_PHPDATE_P(&iterator->current);
16121613
newdateobj->time = timelib_time_ctor();
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
--TEST--
2+
Multiple calls to DatePeriod iterator current() leak objects
3+
--FILE--
4+
<?php
5+
$start = new DateTime('2018-12-31 00:00:00');
6+
$end = new DateTime('2019-12-31 00:00:00');
7+
8+
$interval = new DateInterval('P1M');
9+
$period = new DatePeriod($start, $interval, 1);
10+
11+
$iter = $period->getIterator();
12+
var_dump($iter->current());
13+
var_dump($iter->current());
14+
$iter->current()->setTimestamp(0);
15+
var_dump($iter->current());
16+
17+
?>
18+
--EXPECT--
19+
object(DateTime)#9 (3) {
20+
["date"]=>
21+
string(26) "2018-12-31 00:00:00.000000"
22+
["timezone_type"]=>
23+
int(3)
24+
["timezone"]=>
25+
string(3) "UTC"
26+
}
27+
object(DateTime)#9 (3) {
28+
["date"]=>
29+
string(26) "2018-12-31 00:00:00.000000"
30+
["timezone_type"]=>
31+
int(3)
32+
["timezone"]=>
33+
string(3) "UTC"
34+
}
35+
object(DateTime)#9 (3) {
36+
["date"]=>
37+
string(26) "2018-12-31 00:00:00.000000"
38+
["timezone_type"]=>
39+
int(3)
40+
["timezone"]=>
41+
string(3) "UTC"
42+
}

0 commit comments

Comments
 (0)