Skip to content

Commit 8068a48

Browse files
committed
Fix GH-18422: int overflow in php_date_llabs
php_date_llabs negated its argument with -i, which is UB when i is LLONG_MIN. Cast to unsigned long long before negating and changed the return type to match. Updated Y/x/X format call sites from %lld to %llu. Closes GH-18422
1 parent e166dc8 commit 8068a48

2 files changed

Lines changed: 27 additions & 6 deletions

File tree

ext/date/php_date.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@
3232
#endif
3333

3434
#ifdef PHP_WIN32
35-
static __inline __int64 php_date_llabs( __int64 i ) { return i >= 0? i: -i; }
35+
static __inline unsigned __int64 php_date_llabs( __int64 i ) { return i >= 0 ? (unsigned __int64)i : -(unsigned __int64)i; }
3636
#elif defined(__GNUC__) && __GNUC__ < 3
37-
static __inline __int64_t php_date_llabs( __int64_t i ) { return i >= 0 ? i : -i; }
37+
static __inline unsigned long long php_date_llabs( __int64_t i ) { return i >= 0 ? (unsigned long long)i : -(unsigned long long)i; }
3838
#else
39-
static inline long long php_date_llabs( long long i ) { return i >= 0 ? i : -i; }
39+
static inline unsigned long long php_date_llabs( long long i ) { return i >= 0 ? (unsigned long long)i : -(unsigned long long)i; }
4040
#endif
4141

4242
#ifdef PHP_WIN32
@@ -742,9 +742,9 @@ static zend_string *date_format(const char *format, size_t format_len, const tim
742742
/* year */
743743
case 'L': length = slprintf(buffer, sizeof(buffer), "%d", timelib_is_leap((int) t->y)); break;
744744
case 'y': length = slprintf(buffer, sizeof(buffer), "%02d", (int) (t->y % 100)); break;
745-
case 'Y': length = slprintf(buffer, sizeof(buffer), "%s%04lld", t->y < 0 ? "-" : "", php_date_llabs((timelib_sll) t->y)); break;
746-
case 'x': length = slprintf(buffer, sizeof(buffer), "%s%04lld", t->y < 0 ? "-" : (t->y >= 10000 ? "+" : ""), php_date_llabs((timelib_sll) t->y)); break;
747-
case 'X': length = slprintf(buffer, sizeof(buffer), "%s%04lld", t->y < 0 ? "-" : "+", php_date_llabs((timelib_sll) t->y)); break;
745+
case 'Y': length = slprintf(buffer, sizeof(buffer), "%s%04llu", t->y < 0 ? "-" : "", php_date_llabs((timelib_sll) t->y)); break;
746+
case 'x': length = slprintf(buffer, sizeof(buffer), "%s%04llu", t->y < 0 ? "-" : (t->y >= 10000 ? "+" : ""), php_date_llabs((timelib_sll) t->y)); break;
747+
case 'X': length = slprintf(buffer, sizeof(buffer), "%s%04llu", t->y < 0 ? "-" : "+", php_date_llabs((timelib_sll) t->y)); break;
748748

749749
/* time */
750750
case 'a': length = slprintf(buffer, sizeof(buffer), "%s", t->h >= 12 ? "pm" : "am"); break;

ext/date/tests/gh18422.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
GH-18422 (int overflow in Date extension)
3+
--FILE--
4+
<?php
5+
date_default_timezone_set('UTC');
6+
7+
$dto = date_create("2006-12-12");
8+
date_isodate_set($dto, PHP_INT_MIN, 1, 1);
9+
echo $dto->format("Y"), "\n";
10+
echo $dto->format("x"), "\n";
11+
echo $dto->format("X"), "\n";
12+
13+
echo date_create("2024-06-15")->format("Y"), "\n";
14+
echo date_create("-0042-01-01")->format("Y"), "\n";
15+
?>
16+
--EXPECTF--
17+
-%d
18+
-%d
19+
-%d
20+
2024
21+
-0042

0 commit comments

Comments
 (0)