Skip to content

Commit b48f262

Browse files
committed
Fix #79015: undefined-behavior in php_date.c
We check that the given microsecond fraction is in the valid range [0, 1000000[, and otherwise mark it as invalid. We also drop the useless do loop; a plain block is sufficient here.
1 parent 0cecf83 commit b48f262

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ PHP NEWS
1010
. Implemented FR #77711 (CURLFile should support UNICODE filenames). (cmb)
1111
. Fixed bug #79033 (Curl timeout error with specific url and post). (cmb)
1212

13+
- Date:
14+
. Fixed bug #79015 (undefined-behavior in php_date.c). (cmb)
15+
1316
- Fileinfo:
1417
. Fixed bug #74170 (locale information change after mime_content_type).
1518
(Sergei Turchanov)

ext/date/php_date.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4389,14 +4389,16 @@ static int php_date_interval_initialize_from_hash(zval **return_value, php_inter
43894389
PHP_DATE_INTERVAL_READ_PROPERTY("h", h, timelib_sll, -1)
43904390
PHP_DATE_INTERVAL_READ_PROPERTY("i", i, timelib_sll, -1)
43914391
PHP_DATE_INTERVAL_READ_PROPERTY("s", s, timelib_sll, -1)
4392-
do {
4392+
{
43934393
zval *z_arg = zend_hash_str_find(myht, "f", sizeof("f") - 1);
4394+
(*intobj)->diff->us = -1000000;
43944395
if (z_arg) {
4395-
(*intobj)->diff->us = ((double)zval_get_double(z_arg) * 1000000);
4396-
} else {
4397-
(*intobj)->diff->us = (double) -1000000;
4396+
double val = zval_get_double(z_arg) * 1000000;
4397+
if (val >= 0 && val < 1000000) {
4398+
(*intobj)->diff->us = val;
4399+
}
43984400
}
4399-
} while (0);
4401+
}
44004402
PHP_DATE_INTERVAL_READ_PROPERTY("weekday", weekday, int, -1)
44014403
PHP_DATE_INTERVAL_READ_PROPERTY("weekday_behavior", weekday_behavior, int, -1)
44024404
PHP_DATE_INTERVAL_READ_PROPERTY("first_last_day_of", first_last_day_of, int, -1)

ext/date/tests/bug79015.phpt

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
--TEST--
2+
Bug #79015 (undefined-behavior in php_date.c)
3+
--FILE--
4+
<?php
5+
$payload = 'O:12:"DateInterval":16:{s:1:"y";i:1;s:1:"m";i:0;s:1:"d";i:4;s:1:"h";i:0;s:1:"i";i:0;s:1:"s";i:0;s:1:"f";i:9999999999990;s:7:"weekday";i:0;s:16:"weekday_behavior";i:0;s:17:"first_last_day_of";i:0;s:6:"invert";i:0;s:4:"days";b:0;s:12:"special_type";i:0;s:14:"special_amount";i:0;s:21:"have_weekday_relative";i:0;s:21:"have_special_relative";i:0;}';
6+
var_dump(unserialize($payload));
7+
?>
8+
--EXPECTF--
9+
object(DateInterval)#%d (16) {
10+
["y"]=>
11+
int(1)
12+
["m"]=>
13+
int(0)
14+
["d"]=>
15+
int(4)
16+
["h"]=>
17+
int(0)
18+
["i"]=>
19+
int(0)
20+
["s"]=>
21+
int(0)
22+
["f"]=>
23+
float(-1)
24+
["weekday"]=>
25+
int(0)
26+
["weekday_behavior"]=>
27+
int(0)
28+
["first_last_day_of"]=>
29+
int(0)
30+
["invert"]=>
31+
int(0)
32+
["days"]=>
33+
bool(false)
34+
["special_type"]=>
35+
int(0)
36+
["special_amount"]=>
37+
int(0)
38+
["have_weekday_relative"]=>
39+
int(0)
40+
["have_special_relative"]=>
41+
int(0)
42+
}

0 commit comments

Comments
 (0)