diff --git a/ext/calendar/cal_unix.c b/ext/calendar/cal_unix.c index 78a08fdef0aa5..c3fe5557d244e 100644 --- a/ext/calendar/cal_unix.c +++ b/ext/calendar/cal_unix.c @@ -60,13 +60,13 @@ PHP_FUNCTION(jdtounix) if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &uday) == FAILURE) { RETURN_THROWS(); } - uday -= 2440588 /* J.D. of 1.1.1970 */; - - if (uday < 0 || uday > ZEND_LONG_MAX / SECS_PER_DAY) { /* before beginning of unix epoch or greater than representable */ + if (uday < 2440588 || (uday - 2440588) > (ZEND_LONG_MAX / SECS_PER_DAY)) { /* before beginning of unix epoch or greater than representable */ zend_value_error("jday must be between 2440588 and " ZEND_LONG_FMT, ZEND_LONG_MAX / SECS_PER_DAY + 2440588); RETURN_THROWS(); } + uday -= 2440588 /* J.D. of 1.1.1970 */; + RETURN_LONG(uday * SECS_PER_DAY); } /* }}} */ diff --git a/ext/calendar/tests/gh16231.phpt b/ext/calendar/tests/gh16231.phpt new file mode 100644 index 0000000000000..89b09dd3f63ae --- /dev/null +++ b/ext/calendar/tests/gh16231.phpt @@ -0,0 +1,21 @@ +--TEST-- +GH-16231 (jdtounix argument overflow) +--EXTENSIONS-- +calendar +--FILE-- +getMessage() . PHP_EOL; +} + +try { + jdtounix(240587); +} catch (\ValueError $e) { + echo $e->getMessage(); +} +?> +--EXPECTF-- +jday must be between 2440588 and %d +jday must be between 2440588 and %d