Skip to content

Commit 92d0a10

Browse files
committed
Merge branch 'PHP-7.4' into master
* PHP-7.4: Fix #80185: jdtounix() fails after 2037
2 parents 22ab18c + 9dddfbe commit 92d0a10

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

ext/calendar/cal_unix.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include "sdncal.h"
2222
#include <time.h>
2323

24+
#define SECS_PER_DAY (24 * 3600)
25+
2426
/* {{{ Convert UNIX timestamp to Julian Day */
2527
PHP_FUNCTION(unixtojd)
2628
{
@@ -60,11 +62,11 @@ PHP_FUNCTION(jdtounix)
6062
}
6163
uday -= 2440588 /* J.D. of 1.1.1970 */;
6264

63-
if (uday < 0 || uday > 24755) {
64-
zend_value_error("jday must be within the Unix epoch");
65+
if (uday < 0 || uday > ZEND_LONG_MAX / SECS_PER_DAY) { /* before beginning of unix epoch or greater than representable */
66+
zend_value_error("jday must be between 2440588 and " ZEND_LONG_FMT, ZEND_LONG_MAX / SECS_PER_DAY + 2440588);
6567
RETURN_THROWS();
6668
}
6769

68-
RETURN_LONG(uday * 24 * 3600);
70+
RETURN_LONG(uday * SECS_PER_DAY);
6971
}
7072
/* }}} */

ext/calendar/tests/bug80185.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
Bug #80185 (jdtounix() fails after 2037)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('calendar')) die('skip ext/calendar required');
6+
if (PHP_INT_SIZE != 8) die("skip for 64bit platforms only");
7+
?>
8+
--FILE--
9+
<?php
10+
var_dump(jdtounix(2465712));
11+
var_dump(jdtounix(PHP_INT_MAX / 86400 + 2440588));
12+
try {
13+
var_dump(jdtounix(PHP_INT_MAX / 86400 + 2440589));
14+
} catch (ValueError $ex) {
15+
echo $ex->getMessage(), PHP_EOL;
16+
}
17+
?>
18+
--EXPECT--
19+
int(2170713600)
20+
int(9223372036854720000)
21+
jday must be between 2440588 and 106751993607888
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
Bug #80185 (jdtounix() fails after 2037)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('calendar')) die('skip ext/calendar required');
6+
if (PHP_INT_SIZE != 4) die("skip for 32bit platforms only");
7+
?>
8+
--FILE--
9+
<?php
10+
try {
11+
var_dump(jdtounix(2465712));
12+
} catch (ValueError $ex) {
13+
echo $ex->getMessage(), PHP_EOL;
14+
}
15+
var_dump(jdtounix(PHP_INT_MAX / 86400 + 2440588));
16+
try {
17+
var_dump(jdtounix(PHP_INT_MAX / 86400 + 2440589));
18+
} catch (ValueError $ex) {
19+
echo $ex->getMessage(), PHP_EOL;
20+
}
21+
?>
22+
--EXPECT--
23+
jday must be between 2440588 and 2465443
24+
int(2147472000)
25+
jday must be between 2440588 and 2465443

ext/calendar/tests/jdtounix_error1.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ try {
1515
}
1616
?>
1717
--EXPECT--
18-
jday must be within the Unix epoch
18+
jday must be between 2440588 and 2465443

0 commit comments

Comments
 (0)