Skip to content

Commit f9453a8

Browse files
committed
Fix phpGH-14732: date_sun_info() fails for non-finite values
`timelib_astro_rise_set_altitude()` is not prepared to deal with non- finite values (`nan`, `inf` and `-inf`) for `lon` and `lat`; instead these trigger undefined behavior. Thus we catch non-finite values before even calling that timelib function; for `date_sun_info()` we trigger `ValueError`s; for `date_sunrise()` and `date_sunset()` we silently return `false`, since these functions will be sunsetted anyway. Closes phpGH-16497.
1 parent 886a528 commit f9453a8

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

NEWS

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ PHP NEWS
2828
. Fixed bug GH-16454 (Unhandled INF in date_sunset() with tiny $utcOffset).
2929
(cmb)
3030
. Fixed bug GH-16037 (Assertion failure in ext/date/php_date.c). (Derick)
31+
. Fixed bug GH-14732 (date_sun_info() fails for non-finite values). (cmb)
3132

3233
- DBA:
3334
. Fixed bug GH-16390 (dba_open() can segfault for "pathless" streams). (cmb)

ext/date/php_date.c

+13
Original file line numberDiff line numberDiff line change
@@ -5111,6 +5111,10 @@ static void php_do_date_sunrise_sunset(INTERNAL_FUNCTION_PARAMETERS, bool calc_s
51115111
}
51125112
altitude = 90 - zenith;
51135113

5114+
if (!zend_finite(latitude) || !zend_finite(longitude)) {
5115+
RETURN_FALSE;
5116+
}
5117+
51145118
/* Initialize time struct */
51155119
tzi = get_timezone_info();
51165120
if (!tzi) {
@@ -5188,6 +5192,15 @@ PHP_FUNCTION(date_sun_info)
51885192
Z_PARAM_DOUBLE(longitude)
51895193
ZEND_PARSE_PARAMETERS_END();
51905194

5195+
if (!zend_finite(latitude)) {
5196+
zend_argument_value_error(2, "must be finite");
5197+
RETURN_THROWS();
5198+
}
5199+
if (!zend_finite(longitude)) {
5200+
zend_argument_value_error(3, "must be finite");
5201+
RETURN_THROWS();
5202+
}
5203+
51915204
/* Initialize time struct */
51925205
tzi = get_timezone_info();
51935206
if (!tzi) {

ext/date/tests/gh14732.phpt

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
--TEST--
2+
GH-14732 (date_sun_info() fails for non-finite values)
3+
--FILE--
4+
<?php
5+
try {
6+
date_sun_info(1, NAN, 1);
7+
} catch (ValueError $ex) {
8+
echo $ex->getMessage(), "\n";
9+
}
10+
try {
11+
date_sun_info(1, -INF, 1);
12+
} catch (ValueError $ex) {
13+
echo $ex->getMessage(), "\n";
14+
}
15+
try {
16+
date_sun_info(1, 1, NAN);
17+
} catch (ValueError $ex) {
18+
echo $ex->getMessage(), "\n";
19+
}
20+
try {
21+
date_sun_info(1, 1, INF);
22+
} catch (ValueError $ex) {
23+
echo $ex->getMessage(), "\n";
24+
}
25+
var_dump(date_sunset(1, SUNFUNCS_RET_STRING, NAN, 1));
26+
var_dump(date_sunrise(1, SUNFUNCS_RET_STRING, 1, NAN));
27+
?>
28+
--EXPECTF--
29+
date_sun_info(): Argument #2 ($latitude) must be finite
30+
date_sun_info(): Argument #2 ($latitude) must be finite
31+
date_sun_info(): Argument #3 ($longitude) must be finite
32+
date_sun_info(): Argument #3 ($longitude) must be finite
33+
34+
Deprecated: Function date_sunset() is deprecated in %s on line %d
35+
bool(false)
36+
37+
Deprecated: Function date_sunrise() is deprecated in %s on line %d
38+
bool(false)

0 commit comments

Comments
 (0)