Skip to content

Commit 559eb7f

Browse files
committed
Fix integer overflows in timelib
There are edge cases where computations can cause an integer overflow, which is undefined behaviour. Lately, some fuzzers seem to be hitting these quite frequently. While this behaviour is undefined, it doesn't actually matter in practice, the worst effect is having a wrong computation result, but no sane person would do computations on e.g. the year pow(2,63). Still, undefined behaviour is bad. Make the wrapping behaviour defined by using -fwrapv when possible. The scope of this is limited to timelib and doesn't affect php_date.c. The reason for this is that this may in theory prevent some optimizations and it also seems bad to affect code that lives so close to the PHP-native edge. I tested all issues. This fixes all but one issues, the remaining issue is in php_date.c. Fixes GH-13881. Fixes GH-14075. Fixes GH-15150. Fixes GH-16034. Fixes GH-16035. Fixes GH-16048. Fixes GH-16050. Fixes GH-16051. Fixes GH-16052. Fixes GH-16775. Fixes GH-16864. Fixes GH-16865. Fixes GH-16975. Fixes GH-17025. Fixes GH-17059. Closes GH-17060.
1 parent 2bf3db0 commit 559eb7f

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ PHP NEWS
1616
- Curl:
1717
. Added curl_multi_get_handles(). (timwolla)
1818

19+
- Date:
20+
. Fix undefined behaviour problems regarding integer overflow in extreme edge
21+
cases. (nielsdos, cmb, ilutov)
22+
1923
- DOM:
2024
. Added Dom\Element::$outerHTML. (nielsdos)
2125
. Added Dom\Element::insertAdjacentHTML(). (nielsdos)

ext/date/config.w32

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ PHP_DATE = "yes";
55
ADD_SOURCES("ext/date/lib", "astro.c timelib.c dow.c parse_date.c parse_posix.c parse_tz.c tm2unixtime.c unixtime2tm.c parse_iso_intervals.c interval.c", "date");
66

77
ADD_FLAG('CFLAGS_DATE', "/wd4244");
8+
if (CLANG_TOOLSET) {
9+
ADD_FLAG('CFLAGS_BD_EXT_DATE_LIB', "-fwrapv");
10+
}
811

912
var tl_config = FSO.CreateTextFile("ext/date/lib/timelib_config.h", true);
1013
tl_config.WriteLine("#include \"config.w32.h\"");

ext/date/config0.m4

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,24 @@ AX_CHECK_COMPILE_FLAG([-Wno-implicit-fallthrough],
88
[PHP_DATE_CFLAGS="$PHP_DATE_CFLAGS -Wno-implicit-fallthrough"],,
99
[-Werror])
1010

11-
PHP_DATE_CFLAGS="$PHP_DATE_CFLAGS -I@ext_builddir@/lib -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 -DHAVE_TIMELIB_CONFIG_H=1"
11+
PHP_DATE_CFLAGS="$PHP_DATE_CFLAGS -DHAVE_TIMELIB_CONFIG_H=1"
12+
PHP_TIMELIB_CFLAGS="$PHP_DATE_CFLAGS"
13+
PHP_DATE_CFLAGS="$PHP_DATE_CFLAGS -I@ext_builddir@/lib -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1"
14+
15+
AX_CHECK_COMPILE_FLAG([-fwrapv],
16+
[PHP_TIMELIB_CFLAGS="$PHP_TIMELIB_CFLAGS -fwrapv"],,
17+
[-Werror])
18+
1219
timelib_sources="lib/astro.c lib/dow.c lib/parse_date.c lib/parse_tz.c lib/parse_posix.c
1320
lib/timelib.c lib/tm2unixtime.c lib/unixtime2tm.c lib/parse_iso_intervals.c lib/interval.c"
1421

1522
PHP_NEW_EXTENSION([date],
16-
[php_date.c $timelib_sources],
23+
[php_date.c],
1724
[no],,
1825
[$PHP_DATE_CFLAGS])
1926

27+
PHP_ADD_SOURCES([$ext_dir], [$timelib_sources], [$PHP_TIMELIB_CFLAGS])
28+
2029
PHP_ADD_BUILD_DIR([$ext_builddir/lib], [1])
2130
PHP_ADD_INCLUDE([$ext_builddir/lib])
2231
PHP_ADD_INCLUDE([$ext_srcdir/lib])

0 commit comments

Comments
 (0)