Skip to content

Commit e857dfa

Browse files
committed
Fix #80185: jdtounix() fails after 2037
There is no such thing as the "end of the unix epoch", and if it was, it would certainly not be 2037-10-11T02:00:00. There is, however, potential integer overflow which we need to avoid. Closes GH-6288.
1 parent 69ba81d commit e857dfa

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ PHP NEWS
66
. Fixed bug #79423 (copy command is limited to size of file it can copy).
77
(cmb)
88

9+
- Calendar:
10+
. Fixed bug #80185 (jdtounix() fails after 2037). (cmb)
11+
912
- MySQLnd:
1013
. Fixed bug #80115 (mysqlnd.debug doesn't recognize absolute paths with
1114
slashes). (cmb)

ext/calendar/cal_unix.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include "sdncal.h"
2424
#include <time.h>
2525

26+
#define SECS_PER_DAY (24 * 3600)
27+
2628
/* {{{ proto int unixtojd([int timestamp])
2729
Convert UNIX timestamp to Julian Day */
2830
PHP_FUNCTION(unixtojd)
@@ -62,11 +64,11 @@ PHP_FUNCTION(jdtounix)
6264
}
6365
uday -= 2440588 /* J.D. of 1.1.1970 */;
6466

65-
if (uday < 0 || uday > 24755) { /* before beginning of unix epoch or behind end of unix epoch */
67+
if (uday < 0 || uday > ZEND_LONG_MAX / SECS_PER_DAY) { /* before beginning of unix epoch or greater than representable */
6668
RETURN_FALSE;
6769
}
6870

69-
RETURN_LONG(uday * 24 * 3600);
71+
RETURN_LONG(uday * SECS_PER_DAY);
7072
}
7173
/* }}} */
7274

ext/calendar/tests/bug80185.phpt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
var_dump(jdtounix(PHP_INT_MAX / 86400 + 2440589));
13+
?>
14+
--EXPECT--
15+
int(2170713600)
16+
int(9223372036854720000)
17+
bool(false)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
var_dump(jdtounix(2465712));
11+
var_dump(jdtounix(PHP_INT_MAX / 86400 + 2440588));
12+
var_dump(jdtounix(PHP_INT_MAX / 86400 + 2440589));
13+
?>
14+
--EXPECT--
15+
bool(false)
16+
int(2147472000)
17+
bool(false)

0 commit comments

Comments
 (0)