Skip to content

Commit 48598a2

Browse files
andrewnesterkrakjoe
authored andcommitted
Fixed #74639 - Added proper clone functionality for DatePeriod and DateInterval
1 parent 741769d commit 48598a2

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ PHP NEWS
77
properties). (Laruence)
88
. Fixed misparsing of abstract unix domain socket names. (Sara)
99

10+
- Date:
11+
. Fixed bug #74639 (implement clone for DatePeriod and DateInterval).
12+
(andrewnester)
13+
1014
- Mbstring:
1115
. Add oniguruma upstream fix (CVE-2017-9224, CVE-2017-9226, CVE-2017-9227,
1216
CVE-2017-9228, CVE-2017-9229) (Remi, Mamoru TASAKA)

ext/date/php_date.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2397,8 +2397,11 @@ static zend_object *date_object_clone_interval(zval *this_ptr) /* {{{ */
23972397
php_interval_obj *new_obj = php_interval_obj_from_obj(date_object_new_interval_ex(old_obj->std.ce, 0));
23982398

23992399
zend_objects_clone_members(&new_obj->std, &old_obj->std);
2400+
new_obj->initialized = old_obj->initialized;
2401+
if (old_obj->diff) {
2402+
new_obj->diff = timelib_rel_time_clone(old_obj->diff);
2403+
}
24002404

2401-
/** FIX ME ADD CLONE STUFF **/
24022405
return &new_obj->std;
24032406
} /* }}} */
24042407

@@ -2481,8 +2484,23 @@ static zend_object *date_object_clone_period(zval *this_ptr) /* {{{ */
24812484
php_period_obj *new_obj = php_period_obj_from_obj(date_object_new_period_ex(old_obj->std.ce, 0));
24822485

24832486
zend_objects_clone_members(&new_obj->std, &old_obj->std);
2484-
2485-
/** FIX ME ADD CLONE STUFF **/
2487+
new_obj->initialized = old_obj->initialized;
2488+
new_obj->recurrences = old_obj->recurrences;
2489+
new_obj->include_start_date = old_obj->include_start_date;
2490+
new_obj->start_ce = old_obj->start_ce;
2491+
2492+
if (old_obj->start) {
2493+
new_obj->start = timelib_time_clone(old_obj->start);
2494+
}
2495+
if (old_obj->current) {
2496+
new_obj->current = timelib_time_clone(old_obj->current);
2497+
}
2498+
if (old_obj->end) {
2499+
new_obj->end = timelib_time_clone(old_obj->end);
2500+
}
2501+
if (old_obj->interval) {
2502+
new_obj->interval = timelib_rel_time_clone(old_obj->interval);
2503+
}
24862504
return &new_obj->std;
24872505
} /* }}} */
24882506

ext/date/tests/bug74639.phpt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
Bug #74639 Cloning DatePeriod leads to segfault
3+
--FILE--
4+
<?php
5+
6+
$start = new DateTime('2017-05-22 09:00:00');
7+
$end = new DateTime('2017-08-24 18:00:00');
8+
$interval = $start->diff($end);
9+
10+
$period = new DatePeriod($start, $interval, $end);
11+
$clonedPeriod = clone $period;
12+
$clonedInterval = clone $interval;
13+
14+
if ($period->getStartDate() != $clonedPeriod->getStartDate()) {
15+
echo "failure\n";
16+
}
17+
18+
if ($period->getEndDate() != $clonedPeriod->getEndDate()) {
19+
echo "failure\n";
20+
}
21+
22+
if ($period->getDateInterval() != $clonedPeriod->getDateInterval()) {
23+
echo "failure\n";
24+
}
25+
26+
if ($interval->format('Y-m-d H:i:s') != $clonedInterval->format('Y-m-d H:i:s')) {
27+
echo "failure\n";
28+
}
29+
30+
echo 'success';
31+
?>
32+
--EXPECT--
33+
success

0 commit comments

Comments
 (0)