libc/stdtime: Avoid signed overflow in mktime.
authorzrj <rimvydas.jasinskas@gmail.com>
Fri, 4 May 2018 14:10:08 +0000 (17:10 +0300)
committerzrj <zrj@dragonflybsd.org>
Sat, 5 May 2018 10:43:05 +0000 (13:43 +0300)
Fixes runtime issue with gcc80 -O2 constant folding. We need to use
__generic() macro to properly detect and set LONG_MIN/LONG_MAX here.

lib/libc/stdtime/localtime.c

index ace0935..3ab99ee 100644 (file)
@@ -1830,15 +1830,8 @@ time2sub(struct tm * const tmp,
        /*
        ** Do a binary search (this works whatever time_t's type is).
        */
-       if (!TYPE_SIGNED(time_t)) {
-               lo = 0;
-               hi = lo - 1;
-       } else {
-               lo = 1;
-               for (i = 0; i < (int) TYPE_BIT(time_t) - 1; ++i)
-                       lo *= 2;
-               hi = -(lo + 1);
-       }
+       lo = time_t_min;
+       hi = time_t_max;
        for ( ; ; ) {
                t = lo / 2 + hi / 2;
                if (t < lo)