]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - time/tzfile.c
Update copyright dates with scripts/update-copyrights.
[thirdparty/glibc.git] / time / tzfile.c
index 925f95fb783259371c213800bc66a4b3e8d5e4bd..bcb408fcdba2c4d24e45d9085930c5d9d418ecb4 100644 (file)
@@ -1,5 +1,4 @@
-/* Copyright (C) 1991-1993,1995-2001,2003,2004,2006, 2007
-   Free Software Foundation, Inc.
+/* Copyright (C) 1991-2015 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -13,9 +12,8 @@
    Lesser General Public License for more details.
 
    You should have received a copy of the GNU Lesser General Public
-   License along with the GNU C Library; if not, write to the Free
-   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
-   02111-1307 USA.  */
+   License along with the GNU C Library; if not, see
+   <http://www.gnu.org/licenses/>.  */
 
 #include <assert.h>
 #include <limits.h>
@@ -26,6 +24,7 @@
 #include <time.h>
 #include <unistd.h>
 #include <sys/stat.h>
+#include <stdint.h>
 
 #define        NOID
 #include <timezone/tzfile.h>
@@ -105,16 +104,17 @@ __tzfile_read (const char *file, size_t extra, char **extrap)
 {
   static const char default_tzdir[] = TZDIR;
   size_t num_isstd, num_isgmt;
-  register FILE *f;
+  FILE *f;
   struct tzhead tzhead;
   size_t chars;
-  register size_t i;
+  size_t i;
   size_t total_size;
   size_t types_idx;
   size_t leaps_idx;
   int was_using_tzfile = __use_tzfile;
   int trans_width = 4;
   size_t tzspec_len;
+  char *new = NULL;
 
   if (sizeof (time_t) != 4 && sizeof (time_t) != 8)
     abort ();
@@ -146,22 +146,12 @@ __tzfile_read (const char *file, size_t extra, char **extrap)
   if (*file != '/')
     {
       const char *tzdir;
-      unsigned int len, tzdir_len;
-      char *new, *tmp;
 
       tzdir = getenv ("TZDIR");
       if (tzdir == NULL || *tzdir == '\0')
-       {
-         tzdir = default_tzdir;
-         tzdir_len = sizeof (default_tzdir) - 1;
-       }
-      else
-       tzdir_len = strlen (tzdir);
-      len = strlen (file) + 1;
-      new = (char *) __alloca (tzdir_len + 1 + len);
-      tmp = __mempcpy (new, tzdir, tzdir_len);
-      *tmp++ = '/';
-      memcpy (tmp, file, len);
+       tzdir = default_tzdir;
+      if (__asprintf (&new, "%s/%s", tzdir, file) == -1)
+       goto ret_free_transitions;
       file = new;
     }
 
@@ -171,20 +161,16 @@ __tzfile_read (const char *file, size_t extra, char **extrap)
       && stat64 (file, &st) == 0
       && tzfile_ino == st.st_ino && tzfile_dev == st.st_dev
       && tzfile_mtime == st.st_mtime)
-    {
-      /* Nothing to do.  */
-      __use_tzfile = 1;
-      return;
-    }
+    goto done;  /* Nothing to do.  */
 
   /* Note the file is opened with cancellation in the I/O functions
-     disabled.  */
-  f = fopen (file, "rc");
+     disabled and if available FD_CLOEXEC set.  */
+  f = fopen (file, "rce");
   if (f == NULL)
     goto ret_free_transitions;
 
   /* Get information about the file we are actually using.  */
-  if (fstat64 (fileno (f), &st) != 0)
+  if (fstat64 (__fileno (f), &st) != 0)
     {
       fclose (f);
       goto ret_free_transitions;
@@ -202,8 +188,8 @@ __tzfile_read (const char *file, size_t extra, char **extrap)
   __fsetlocking (f, FSETLOCKING_BYCALLER);
 
  read_again:
-  if (__builtin_expect (fread_unlocked ((void *) &tzhead, sizeof (tzhead),
-                                       1, f) != 1, 0)
+  if (__builtin_expect (__fread_unlocked ((void *) &tzhead, sizeof (tzhead),
+                                         1, f) != 1, 0)
       || memcmp (tzhead.tzh_magic, TZ_MAGIC, sizeof (tzhead.tzh_magic)) != 0)
     goto lose;
 
@@ -234,23 +220,58 @@ __tzfile_read (const char *file, size_t extra, char **extrap)
       goto read_again;
     }
 
+  if (__builtin_expect (num_transitions
+                       > ((SIZE_MAX - (__alignof__ (struct ttinfo) - 1))
+                          / (sizeof (time_t) + 1)), 0))
+    goto lose;
   total_size = num_transitions * (sizeof (time_t) + 1);
   total_size = ((total_size + __alignof__ (struct ttinfo) - 1)
                & ~(__alignof__ (struct ttinfo) - 1));
   types_idx = total_size;
-  total_size += num_types * sizeof (struct ttinfo) + chars;
+  if (__builtin_expect (num_types
+                       > (SIZE_MAX - total_size) / sizeof (struct ttinfo), 0))
+    goto lose;
+  total_size += num_types * sizeof (struct ttinfo);
+  if (__glibc_unlikely (chars > SIZE_MAX - total_size))
+    goto lose;
+  total_size += chars;
+  if (__builtin_expect (__alignof__ (struct leap) - 1
+                       > SIZE_MAX - total_size, 0))
+    goto lose;
   total_size = ((total_size + __alignof__ (struct leap) - 1)
                & ~(__alignof__ (struct leap) - 1));
   leaps_idx = total_size;
+  if (__builtin_expect (num_leaps
+                       > (SIZE_MAX - total_size) / sizeof (struct leap), 0))
+    goto lose;
   total_size += num_leaps * sizeof (struct leap);
-  tzspec_len = (trans_width == 8
-               ? st.st_size - (ftello (f)
-                               + num_transitions * (8 + 1)
-                               + num_types * 6
-                               + chars
-                               + num_leaps * 8
-                               + num_isstd
-                               + num_isgmt) - 1 : 0);
+  tzspec_len = 0;
+  if (sizeof (time_t) == 8 && trans_width == 8)
+    {
+      off_t rem = st.st_size - __ftello (f);
+      if (__builtin_expect (rem < 0
+                           || (size_t) rem < (num_transitions * (8 + 1)
+                                              + num_types * 6
+                                              + chars), 0))
+       goto lose;
+      tzspec_len = (size_t) rem - (num_transitions * (8 + 1)
+                                  + num_types * 6
+                                  + chars);
+      if (__builtin_expect (num_leaps > SIZE_MAX / 12
+                           || tzspec_len < num_leaps * 12, 0))
+       goto lose;
+      tzspec_len -= num_leaps * 12;
+      if (__glibc_unlikely (tzspec_len < num_isstd))
+       goto lose;
+      tzspec_len -= num_isstd;
+      if (__glibc_unlikely (tzspec_len == 0 || tzspec_len - 1 < num_isgmt))
+       goto lose;
+      tzspec_len -= num_isgmt + 1;
+      if (__glibc_unlikely (SIZE_MAX - total_size < tzspec_len))
+       goto lose;
+    }
+  if (__glibc_unlikely (SIZE_MAX - total_size - tzspec_len < extra))
+    goto lose;
 
   /* Allocate enough memory including the extra block requested by the
      caller.  */
@@ -263,33 +284,34 @@ __tzfile_read (const char *file, size_t extra, char **extrap)
   types = (struct ttinfo *) ((char *) transitions + types_idx);
   zone_names = (char *) types + num_types * sizeof (struct ttinfo);
   leaps = (struct leap *) ((char *) transitions + leaps_idx);
-  if (trans_width == 8)
+  if (sizeof (time_t) == 8 && trans_width == 8)
     tzspec = (char *) leaps + num_leaps * sizeof (struct leap) + extra;
   else
     tzspec = NULL;
   if (extra > 0)
     *extrap = (char *) &leaps[num_leaps];
 
-  if (sizeof (time_t) == 4 || trans_width == 8)
+  if (sizeof (time_t) == 4 || __builtin_expect (trans_width == 8, 1))
     {
-      if (__builtin_expect (fread_unlocked (transitions, trans_width + 1,
-                                           num_transitions, f)
+      if (__builtin_expect (__fread_unlocked (transitions, trans_width + 1,
+                                             num_transitions, f)
                            != num_transitions, 0))
        goto lose;
     }
   else
     {
-      if (__builtin_expect (fread_unlocked (transitions, 4, num_transitions, f)
+      if (__builtin_expect (__fread_unlocked (transitions, 4,
+                                             num_transitions, f)
                            != num_transitions, 0)
-         || __builtin_expect (fread_unlocked (type_idxs, 1, num_transitions,
-                                              f) != num_transitions, 0))
+         || __builtin_expect (__fread_unlocked (type_idxs, 1, num_transitions,
+                                                f) != num_transitions, 0))
        goto lose;
     }
 
   /* Check for bogus indices in the data file, so we can hereafter
      safely use type_idxs[T] as indices into `types' and never crash.  */
   for (i = 0; i < num_transitions; ++i)
-    if (__builtin_expect (type_idxs[i] >= num_types, 0))
+    if (__glibc_unlikely (type_idxs[i] >= num_types))
       goto lose;
 
   if ((BYTE_ORDER != BIG_ENDIAN && (sizeof (time_t) == 4 || trans_width == 4))
@@ -316,28 +338,29 @@ __tzfile_read (const char *file, size_t extra, char **extrap)
     {
       unsigned char x[4];
       int c;
-      if (__builtin_expect (fread_unlocked (x, 1, sizeof (x), f) != sizeof (x),
+      if (__builtin_expect (__fread_unlocked (x, 1,
+                                             sizeof (x), f) != sizeof (x),
                            0))
        goto lose;
       c = getc_unlocked (f);
-      if (__builtin_expect ((unsigned int) c > 1u, 0))
+      if (__glibc_unlikely ((unsigned int) c > 1u))
        goto lose;
       types[i].isdst = c;
       c = getc_unlocked (f);
-      if (__builtin_expect ((size_t) c > chars, 0))
+      if (__glibc_unlikely ((size_t) c > chars))
        /* Bogus index in data file.  */
        goto lose;
       types[i].idx = c;
       types[i].offset = (long int) decode (x);
     }
 
-  if (__builtin_expect (fread_unlocked (zone_names, 1, chars, f) != chars, 0))
+  if (__glibc_unlikely (__fread_unlocked (zone_names, 1, chars, f) != chars))
     goto lose;
 
   for (i = 0; i < num_leaps; ++i)
     {
       unsigned char x[8];
-      if (__builtin_expect (fread_unlocked (x, 1, trans_width, f)
+      if (__builtin_expect (__fread_unlocked (x, 1, trans_width, f)
                            != trans_width, 0))
        goto lose;
       if (sizeof (time_t) == 4 || trans_width == 4)
@@ -345,7 +368,7 @@ __tzfile_read (const char *file, size_t extra, char **extrap)
       else
        leaps[i].transition = (time_t) decode64 (x);
 
-      if (__builtin_expect (fread_unlocked (x, 1, 4, f) != 4, 0))
+      if (__glibc_unlikely (__fread_unlocked (x, 1, 4, f) != 4))
        goto lose;
       leaps[i].change = (long int) decode (x);
     }
@@ -353,7 +376,7 @@ __tzfile_read (const char *file, size_t extra, char **extrap)
   for (i = 0; i < num_isstd; ++i)
     {
       int c = getc_unlocked (f);
-      if (__builtin_expect (c == EOF, 0))
+      if (__glibc_unlikely (c == EOF))
        goto lose;
       types[i].isstd = c != 0;
     }
@@ -363,7 +386,7 @@ __tzfile_read (const char *file, size_t extra, char **extrap)
   for (i = 0; i < num_isgmt; ++i)
     {
       int c = getc_unlocked (f);
-      if (__builtin_expect (c == EOF, 0))
+      if (__glibc_unlikely (c == EOF))
        goto lose;
       types[i].isgmt = c != 0;
     }
@@ -371,15 +394,58 @@ __tzfile_read (const char *file, size_t extra, char **extrap)
     types[i++].isgmt = 0;
 
   /* Read the POSIX TZ-style information if possible.  */
-  if (tzspec != NULL)
+  if (sizeof (time_t) == 8 && tzspec != NULL)
     {
       /* Skip over the newline first.  */
       if (getc_unlocked (f) != '\n'
-         || fread_unlocked (tzspec, 1, tzspec_len - 1, f) != tzspec_len - 1)
+         || (__fread_unlocked (tzspec, 1, tzspec_len - 1, f)
+             != tzspec_len - 1))
        tzspec = NULL;
       else
        tzspec[tzspec_len - 1] = '\0';
     }
+  else if (sizeof (time_t) == 4 && tzhead.tzh_version[0] != '\0')
+    {
+      /* Get the TZ string.  */
+      if (__builtin_expect (__fread_unlocked ((void *) &tzhead,
+                                             sizeof (tzhead), 1, f) != 1, 0)
+         || (memcmp (tzhead.tzh_magic, TZ_MAGIC, sizeof (tzhead.tzh_magic))
+             != 0))
+       goto lose;
+
+      size_t num_transitions2 = (size_t) decode (tzhead.tzh_timecnt);
+      size_t num_types2 = (size_t) decode (tzhead.tzh_typecnt);
+      size_t chars2 = (size_t) decode (tzhead.tzh_charcnt);
+      size_t num_leaps2 = (size_t) decode (tzhead.tzh_leapcnt);
+      size_t num_isstd2 = (size_t) decode (tzhead.tzh_ttisstdcnt);
+      size_t num_isgmt2 = (size_t) decode (tzhead.tzh_ttisgmtcnt);
+
+      /* Position the stream before the second header.  */
+      size_t to_skip = (num_transitions2 * (8 + 1)
+                       + num_types2 * 6
+                       + chars2
+                       + num_leaps2 * 12
+                       + num_isstd2
+                       + num_isgmt2);
+      off_t off;
+      if (fseek (f, to_skip, SEEK_CUR) != 0
+         || (off = __ftello (f)) < 0
+         || st.st_size < off + 2)
+       goto lose;
+
+      tzspec_len = st.st_size - off - 1;
+      char *tzstr = alloca (tzspec_len);
+      if (getc_unlocked (f) != '\n'
+         || (__fread_unlocked (tzstr, 1, tzspec_len - 1, f)
+             != tzspec_len - 1))
+       goto lose;
+      tzstr[tzspec_len - 1] = '\0';
+      tzspec = __tzstring (tzstr);
+    }
+
+  /* Don't use an empty TZ string.  */
+  if (tzspec != NULL && tzspec[0] == '\0')
+    tzspec = NULL;
 
   fclose (f);
 
@@ -451,12 +517,15 @@ __tzfile_read (const char *file, size_t extra, char **extrap)
   __daylight = rule_stdoff != rule_dstoff;
   __timezone = -rule_stdoff;
 
+ done:
   __use_tzfile = 1;
+  free (new);
   return;
 
  lose:
   fclose (f);
  ret_free_transitions:
+  free (new);
   free ((void *) transitions);
   transitions = NULL;
 }
@@ -554,14 +623,14 @@ __tzfile_compute (time_t timer, int use_localtime,
                  long int *leap_correct, int *leap_hit,
                  struct tm *tp)
 {
-  register size_t i;
+  size_t i;
 
   if (use_localtime)
     {
       __tzname[0] = NULL;
       __tzname[1] = NULL;
 
-      if (num_transitions == 0 || timer < transitions[0])
+      if (__glibc_unlikely (num_transitions == 0 || timer < transitions[0]))
        {
          /* TIMER is before any transition (or there are no transitions).
             Choose the first non-DST type
@@ -591,9 +660,9 @@ __tzfile_compute (time_t timer, int use_localtime,
                  ++j;
            }
        }
-      else if (timer >= transitions[num_transitions - 1])
+      else if (__glibc_unlikely (timer >= transitions[num_transitions - 1]))
        {
-         if (tzspec == NULL)
+         if (__glibc_unlikely (tzspec == NULL))
            {
            use_last:
              i = num_transitions;
@@ -605,15 +674,23 @@ __tzfile_compute (time_t timer, int use_localtime,
 
          /* Convert to broken down structure.  If this fails do not
             use the string.  */
-         if (! __offtime (&timer, 0, tp))
+         if (__glibc_unlikely (! __offtime (&timer, 0, tp)))
            goto use_last;
 
          /* Use the rules from the TZ string to compute the change.  */
          __tz_compute (timer, tp, 1);
 
-         *leap_correct = 0L;
-         *leap_hit = 0;
-         return;
+         /* If tzspec comes from posixrules loaded by __tzfile_default,
+            override the STD and DST zone names with the ones user
+            requested in TZ envvar.  */
+         if (__glibc_unlikely (zone_names == (char *) &leaps[num_leaps]))
+           {
+             assert (num_types == 2);
+             __tzname[0] = __tzstring (zone_names);
+             __tzname[1] = __tzstring (&zone_names[strlen (zone_names) + 1]);
+           }
+
+         goto leap;
        }
       else
        {
@@ -688,6 +765,9 @@ __tzfile_compute (time_t timer, int use_localtime,
              ++j;
            }
 
+         if (__glibc_unlikely (__tzname[0] == NULL))
+           __tzname[0] = __tzname[1];
+
          i = type_idxs[i - 1];
        }
 
@@ -711,6 +791,7 @@ __tzfile_compute (time_t timer, int use_localtime,
       tp->tm_gmtoff = info->offset;
     }
 
+ leap:
   *leap_correct = 0L;
   *leap_hit = 0;