Skip to content

Commit 800bd97

Browse files
committed
Ensure the internal properties cannot be overwritten
1 parent 25ca104 commit 800bd97

File tree

2 files changed

+80
-3
lines changed

2 files changed

+80
-3
lines changed

ext/date/php_date.c

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5310,12 +5310,35 @@ PHP_METHOD(DatePeriod, __wakeup)
53105310
}
53115311
/* }}} */
53125312

5313+
/* {{{ date_period_magic_property
5314+
* Common for date_period_read_property() and date_period_write_property() functions
5315+
*/
5316+
static int date_period_is_magic_property(zend_string *name)
5317+
{
5318+
if (zend_string_equals_literal(name, "recurrences")
5319+
|| zend_string_equals_literal(name, "include_start_date")
5320+
|| zend_string_equals_literal(name, "start")
5321+
|| zend_string_equals_literal(name, "current")
5322+
|| zend_string_equals_literal(name, "end")
5323+
|| zend_string_equals_literal(name, "interval")
5324+
) {
5325+
return 1;
5326+
}
5327+
return 0;
5328+
}
5329+
/* }}} */
5330+
53135331
/* {{{ date_period_read_property */
53145332
static zval *date_period_read_property(zval *object, zval *member, int type, void **cache_slot, zval *rv)
53155333
{
53165334
if (type != BP_VAR_IS && type != BP_VAR_R) {
5317-
zend_throw_error(NULL, "Retrieval of DatePeriod properties for modification is unsupported");
5318-
return &EG(uninitialized_zval);
5335+
zend_string *name = zval_get_string(member);
5336+
if (date_period_is_magic_property(name)) {
5337+
zend_throw_error(NULL, "Retrieval of DatePeriod->%s for modification is unsupported", ZSTR_VAL(name));
5338+
zend_string_release(name);
5339+
return &EG(uninitialized_zval);
5340+
}
5341+
zend_string_release(name);
53195342
}
53205343

53215344
Z_OBJPROP_P(object); /* build properties hash table */
@@ -5327,7 +5350,15 @@ static zval *date_period_read_property(zval *object, zval *member, int type, voi
53275350
/* {{{ date_period_write_property */
53285351
static void date_period_write_property(zval *object, zval *member, zval *value, void **cache_slot)
53295352
{
5330-
zend_throw_error(NULL, "Writing to DatePeriod properties is unsupported");
5353+
zend_string *name = zval_get_string(member);
5354+
if (date_period_is_magic_property(name)) {
5355+
zend_throw_error(NULL, "Writing to DatePeriod->%s is unsupported", ZSTR_VAL(name));
5356+
zend_string_release(name);
5357+
return;
5358+
}
5359+
zend_string_release(name);
5360+
5361+
std_object_handlers.write_property(object, member, value, cache_slot);
53315362
}
53325363
/* }}} */
53335364

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
--TEST--
2+
DatePeriod: Test cannot modify read only properties
3+
--INI--
4+
date.timezone=UTC
5+
--FILE--
6+
<?php
7+
8+
$period = new DatePeriod(new DateTime, new DateInterval('P1D'), new DateTime);
9+
10+
$properties = [
11+
"recurrences",
12+
"include_start_date",
13+
"start",
14+
"current",
15+
"end",
16+
"interval",
17+
];
18+
19+
foreach ($properties as $property) {
20+
try {
21+
$period->$property = "new";
22+
} catch (Error $e) {
23+
echo $e->getMessage() . "\n";
24+
}
25+
26+
try {
27+
$period->$property[] = "extra";
28+
} catch (Error $e) {
29+
echo $e->getMessage() . "\n";
30+
}
31+
}
32+
33+
?>
34+
--EXPECT--
35+
Writing to DatePeriod->recurrences is unsupported
36+
Retrieval of DatePeriod->recurrences for modification is unsupported
37+
Writing to DatePeriod->include_start_date is unsupported
38+
Retrieval of DatePeriod->include_start_date for modification is unsupported
39+
Writing to DatePeriod->start is unsupported
40+
Retrieval of DatePeriod->start for modification is unsupported
41+
Writing to DatePeriod->current is unsupported
42+
Retrieval of DatePeriod->current for modification is unsupported
43+
Writing to DatePeriod->end is unsupported
44+
Retrieval of DatePeriod->end for modification is unsupported
45+
Writing to DatePeriod->interval is unsupported
46+
Retrieval of DatePeriod->interval for modification is unsupported

0 commit comments

Comments
 (0)