fix TZ parsing logic for identifying POSIX-form strings
[musl] / src / time / __tz.c
index 92c43a5..3e2fcdc 100644 (file)
@@ -3,7 +3,16 @@
 #include <limits.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/mman.h>
+#include <ctype.h>
 #include "libc.h"
+#include "lock.h"
+#include "fork_impl.h"
+
+#define malloc __libc_malloc
+#define calloc undef
+#define realloc undef
+#define free undef
 
 long  __timezone = 0;
 int   __daylight = 0;
@@ -15,7 +24,7 @@ weak_alias(__tzname, tzname);
 
 static char std_name[TZNAME_MAX+1];
 static char dst_name[TZNAME_MAX+1];
-const char __gmt[] = "GMT";
+const char __utc[] = "UTC";
 
 static int dst_off;
 static int r0[5], r1[5];
@@ -27,7 +36,8 @@ static char old_tz_buf[32];
 static char *old_tz = old_tz_buf;
 static size_t old_tz_size = sizeof old_tz_buf;
 
-static int lock[2];
+static volatile int lock[1];
+volatile int *const __timezone_lockptr = lock;
 
 static int getint(const char **p)
 {
@@ -36,19 +46,16 @@ static int getint(const char **p)
        return x;
 }
 
-static int getsigned(const char **p)
+static int getoff(const char **p)
 {
+       int neg = 0;
        if (**p == '-') {
                ++*p;
-               return -getint(p);
+               neg = 1;
+       } else if (**p == '+') {
+               ++*p;
        }
-       if (**p == '+') ++*p;
-       return getint(p);
-}
-
-static int getoff(const char **p)
-{
-       int off = 3600*getsigned(p);
+       int off = 3600*getint(p);
        if (**p == ':') {
                ++*p;
                off += 60*getint(p);
@@ -57,7 +64,7 @@ static int getoff(const char **p)
                        off += getint(p);
                }
        }
-       return off;
+       return neg ? -off : off;
 }
 
 static void getrule(const char **p, int rule[5])
@@ -87,15 +94,15 @@ static void getname(char *d, const char **p)
        int i;
        if (**p == '<') {
                ++*p;
-               for (i=0; **p!='>' && i<TZNAME_MAX; i++)
-                       d[i] = (*p)[i];
-               ++*p;
+               for (i=0; (*p)[i] && (*p)[i]!='>'; i++)
+                       if (i<TZNAME_MAX) d[i] = (*p)[i];
+               if ((*p)[i]) ++*p;
        } else {
-               for (i=0; ((*p)[i]|32)-'a'<26U && i<TZNAME_MAX; i++)
-                       d[i] = (*p)[i];
+               for (i=0; ((*p)[i]|32)-'a'<26U; i++)
+                       if (i<TZNAME_MAX) d[i] = (*p)[i];
        }
        *p += i;
-       d[i] = 0;
+       d[i<TZNAME_MAX?i:TZNAME_MAX] = 0;
 }
 
 #define VEC(...) ((const unsigned char[]){__VA_ARGS__})
@@ -116,8 +123,6 @@ static size_t zi_dotprod(const unsigned char *z, const unsigned char *v, size_t
        return y;
 }
 
-int __munmap(void *, size_t);
-
 static void do_tzset()
 {
        char buf[NAME_MAX+25], *pathname=buf+24;
@@ -128,17 +133,20 @@ static void do_tzset()
                "/usr/share/zoneinfo/\0/share/zoneinfo/\0/etc/zoneinfo/\0";
 
        s = getenv("TZ");
-       if (!s || !*s) s = "/etc/localtime";
+       if (!s) s = "/etc/localtime";
+       if (!*s) s = __utc;
 
        if (old_tz && !strcmp(s, old_tz)) return;
 
+       for (i=0; i<5; i++) r0[i] = r1[i] = 0;
+
        if (zi) __munmap((void *)zi, map_size);
 
        /* Cache the old value of TZ to check if it has changed. Avoid
         * free so as not to pull it into static programs. Growth
         * strategy makes it so free would have minimal benefit anyway. */
        i = strlen(s);
-       if (i > PATH_MAX+1) s = __gmt, i = 3;
+       if (i > PATH_MAX+1) s = __utc, i = 3;
        if (i >= old_tz_size) {
                old_tz_size *= 2;
                if (i >= old_tz_size) old_tz_size = i+1;
@@ -147,10 +155,21 @@ static void do_tzset()
        }
        if (old_tz) memcpy(old_tz, s, i+1);
 
+       int posix_form = 0;
+       if (*s != ':') {
+               p = s;
+               char dummy_name[TZNAME_MAX+1];
+               getname(dummy_name, &p);
+               if (p!=s && (*p == '+' || *p == '-' || isdigit(*p)
+                            || !strcmp(dummy_name, "UTC")
+                            || !strcmp(dummy_name, "GMT")))
+                       posix_form = 1;
+       }       
+
        /* Non-suid can use an absolute tzfile pathname or a relative
         * pathame beginning with "."; in secure mode, only the
         * standard path will be searched. */
-       if (*s == ':' || ((p=strchr(s, '/')) && !memchr(s, ',', p-s))) {
+       if (!posix_form) {
                if (*s == ':') s++;
                if (*s == '/' || *s == '.') {
                        if (!libc.secure || !strcmp(s, "/etc/localtime"))
@@ -167,18 +186,18 @@ static void do_tzset()
                                }
                        }
                }
-               if (!map) s = __gmt;
+               if (!map) s = __utc;
        }
        if (map && (map_size < 44 || memcmp(map, "TZif", 4))) {
                __munmap((void *)map, map_size);
                map = 0;
-               s = __gmt;
+               s = __utc;
        }
 
        zi = map;
        if (map) {
                int scale = 2;
-               if (sizeof(time_t) > 4 && map[4]=='2') {
+               if (map[4]!='1') {
                        size_t skip = zi_dotprod(zi+20, VEC(1,1,8,5,6,1), 6);
                        trans = zi+skip+44+44;
                        scale++;
@@ -196,7 +215,6 @@ static void do_tzset()
                        const unsigned char *p;
                        __tzname[0] = __tzname[1] = 0;
                        __daylight = __timezone = dst_off = 0;
-                       for (i=0; i<5; i++) r0[i] = r1[i] = 0;
                        for (p=types; p<abbrevs; p+=6) {
                                if (!p[4] && !__tzname[0]) {
                                        __tzname[0] = (char *)abbrevs + p[5];
@@ -209,7 +227,7 @@ static void do_tzset()
                                }
                        }
                        if (!__tzname[0]) __tzname[0] = __tzname[1];
-                       if (!__tzname[0]) __tzname[0] = (char *)__gmt;
+                       if (!__tzname[0]) __tzname[0] = (char *)__utc;
                        if (!__daylight) {
                                __tzname[1] = __tzname[0];
                                dst_off = __timezone;
@@ -218,7 +236,7 @@ static void do_tzset()
                }
        }
 
-       if (!s) s = __gmt;
+       if (!s) s = __utc;
        getname(std_name, &s);
        __tzname[0] = std_name;
        __timezone = getoff(&s);
@@ -232,7 +250,7 @@ static void do_tzset()
                        dst_off = __timezone - 3600;
        } else {
                __daylight = 0;
-               dst_off = 0;
+               dst_off = __timezone;
        }
 
        if (*s == ',') s++, getrule(&s, r0);
@@ -356,9 +374,9 @@ void __secs_to_zone(long long t, int local, int *isdst, long *offset, long *oppo
                size_t alt, i = scan_trans(t, local, &alt);
                if (i != -1) {
                        *isdst = types[6*i+4];
-                       *offset = -(int32_t)zi_read32(types+6*i);
+                       *offset = (int32_t)zi_read32(types+6*i);
                        *zonename = (const char *)abbrevs + types[6*i+5];
-                       if (oppoff) *oppoff = -(int32_t)zi_read32(types+6*alt);
+                       if (oppoff) *oppoff = (int32_t)zi_read32(types+6*alt);
                        UNLOCK(lock);
                        return;
                }
@@ -375,37 +393,33 @@ void __secs_to_zone(long long t, int local, int *isdst, long *offset, long *oppo
        long long t0 = rule_to_secs(r0, y);
        long long t1 = rule_to_secs(r1, y);
 
+       if (!local) {
+               t0 += __timezone;
+               t1 += dst_off;
+       }
        if (t0 < t1) {
-               if (!local) {
-                       t0 += __timezone;
-                       t1 += dst_off;
-               }
                if (t >= t0 && t < t1) goto dst;
                goto std;
        } else {
-               if (!local) {
-                       t1 += __timezone;
-                       t0 += dst_off;
-               }
                if (t >= t1 && t < t0) goto std;
                goto dst;
        }
 std:
        *isdst = 0;
-       *offset = __timezone;
-       if (oppoff) *oppoff = dst_off;
+       *offset = -__timezone;
+       if (oppoff) *oppoff = -dst_off;
        *zonename = __tzname[0];
        UNLOCK(lock);
        return;
 dst:
        *isdst = 1;
-       *offset = dst_off;
-       if (oppoff) *oppoff = __timezone;
+       *offset = -dst_off;
+       if (oppoff) *oppoff = -__timezone;
        *zonename = __tzname[1];
        UNLOCK(lock);
 }
 
-void __tzset()
+static void __tzset()
 {
        LOCK(lock);
        do_tzset();
@@ -419,7 +433,7 @@ const char *__tm_to_tzname(const struct tm *tm)
        const void *p = tm->__tm_zone;
        LOCK(lock);
        do_tzset();
-       if (p != __gmt && p != __tzname[0] && p != __tzname[1] &&
+       if (p != __utc && p != __tzname[0] && p != __tzname[1] &&
            (!zi || (uintptr_t)p-(uintptr_t)abbrevs >= abbrevs_end - abbrevs))
                p = "";
        UNLOCK(lock);