]> git.ipfire.org Git - thirdparty/glibc.git/blobdiff - time/tzfile.c
Installed-header hygiene (BZ#20366): time.h types.
[thirdparty/glibc.git] / time / tzfile.c
index 9dd5130757b30c16a402a300bc8e354e06f1defb..90498783993cc5ef44aff06afe7cec5550ca8096 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 1991-2013 Free Software Foundation, Inc.
+/* Copyright (C) 1991-2016 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
@@ -114,6 +114,7 @@ __tzfile_read (const char *file, size_t extra, char **extrap)
   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 ();
@@ -145,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;
     }
 
@@ -170,11 +161,7 @@ __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 and if available FD_CLOEXEC set.  */
@@ -183,7 +170,7 @@ __tzfile_read (const char *file, size_t extra, char **extrap)
     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;
@@ -201,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;
 
@@ -213,6 +200,9 @@ __tzfile_read (const char *file, size_t extra, char **extrap)
   num_isstd = (size_t) decode (tzhead.tzh_ttisstdcnt);
   num_isgmt = (size_t) decode (tzhead.tzh_ttisgmtcnt);
 
+  if (__glibc_unlikely (num_isstd > num_types || num_isgmt > num_types))
+    goto lose;
+
   /* For platforms with 64-bit time_t we use the new format if available.  */
   if (sizeof (time_t) == 8 && trans_width == 4
       && tzhead.tzh_version[0] != '\0')
@@ -245,7 +235,7 @@ __tzfile_read (const char *file, size_t extra, char **extrap)
                        > (SIZE_MAX - total_size) / sizeof (struct ttinfo), 0))
     goto lose;
   total_size += num_types * sizeof (struct ttinfo);
-  if (__builtin_expect (chars > SIZE_MAX - total_size, 0))
+  if (__glibc_unlikely (chars > SIZE_MAX - total_size))
     goto lose;
   total_size += chars;
   if (__builtin_expect (__alignof__ (struct leap) - 1
@@ -261,7 +251,7 @@ __tzfile_read (const char *file, size_t extra, char **extrap)
   tzspec_len = 0;
   if (sizeof (time_t) == 8 && trans_width == 8)
     {
-      off_t rem = st.st_size - ftello (f);
+      off_t rem = st.st_size - __ftello (f);
       if (__builtin_expect (rem < 0
                            || (size_t) rem < (num_transitions * (8 + 1)
                                               + num_types * 6
@@ -274,16 +264,17 @@ __tzfile_read (const char *file, size_t extra, char **extrap)
                            || tzspec_len < num_leaps * 12, 0))
        goto lose;
       tzspec_len -= num_leaps * 12;
-      if (__builtin_expect (tzspec_len < num_isstd, 0))
+      if (__glibc_unlikely (tzspec_len < num_isstd))
        goto lose;
       tzspec_len -= num_isstd;
-      if (__builtin_expect (tzspec_len == 0 || tzspec_len - 1 < num_isgmt, 0))
+      if (__glibc_unlikely (tzspec_len == 0 || tzspec_len - 1 < num_isgmt))
        goto lose;
       tzspec_len -= num_isgmt + 1;
-      if (__builtin_expect (SIZE_MAX - total_size < tzspec_len, 0))
+      if (__glibc_unlikely (tzspec_len == 0
+                           || SIZE_MAX - total_size < tzspec_len))
        goto lose;
     }
-  if (__builtin_expect (SIZE_MAX - total_size - tzspec_len < extra, 0))
+  if (__glibc_unlikely (SIZE_MAX - total_size - tzspec_len < extra))
     goto lose;
 
   /* Allocate enough memory including the extra block requested by the
@@ -306,24 +297,25 @@ __tzfile_read (const char *file, size_t extra, char **extrap)
 
   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))
@@ -350,28 +342,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)
@@ -379,7 +372,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);
     }
@@ -387,7 +380,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;
     }
@@ -397,7 +390,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;
     }
@@ -409,7 +402,7 @@ __tzfile_read (const char *file, size_t extra, char **extrap)
     {
       /* Skip over the newline first.  */
       if (getc_unlocked (f) != '\n'
-         || (fread_unlocked (tzspec, 1, tzspec_len - 1, f)
+         || (__fread_unlocked (tzspec, 1, tzspec_len - 1, f)
              != tzspec_len - 1))
        tzspec = NULL;
       else
@@ -418,8 +411,8 @@ __tzfile_read (const char *file, size_t extra, char **extrap)
   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)
+      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;
@@ -440,17 +433,26 @@ __tzfile_read (const char *file, size_t extra, char **extrap)
                        + num_isgmt2);
       off_t off;
       if (fseek (f, to_skip, SEEK_CUR) != 0
-         || (off = ftello (f)) < 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))
+      if (tzspec_len == 0)
        goto lose;
+      char *tzstr = malloc (tzspec_len);
+      if (tzstr == NULL)
+       goto lose;
+      if (getc_unlocked (f) != '\n'
+         || (__fread_unlocked (tzstr, 1, tzspec_len - 1, f)
+             != tzspec_len - 1))
+       {
+         free (tzstr);
+         goto lose;
+       }
       tzstr[tzspec_len - 1] = '\0';
       tzspec = __tzstring (tzstr);
+      free (tzstr);
     }
 
   /* Don't use an empty TZ string.  */
@@ -527,12 +529,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;
 }
@@ -623,6 +628,12 @@ __tzfile_default (const char *std, const char *dst,
   __timezone = -types[0].offset;
 
   compute_tzname_max (stdlen + dstlen);
+
+  /* Invalidate the tzfile attribute cache to force rereading
+     TZDEFRULES the next time it is used.  */
+  tzfile_dev = 0;
+  tzfile_ino = 0;
+  tzfile_mtime = 0;
 }
 \f
 void
@@ -637,7 +648,7 @@ __tzfile_compute (time_t timer, int use_localtime,
       __tzname[0] = NULL;
       __tzname[1] = NULL;
 
-      if (__builtin_expect (num_transitions == 0 || timer < transitions[0], 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
@@ -667,9 +678,9 @@ __tzfile_compute (time_t timer, int use_localtime,
                  ++j;
            }
        }
-      else if (__builtin_expect (timer >= transitions[num_transitions - 1], 0))
+      else if (__glibc_unlikely (timer >= transitions[num_transitions - 1]))
        {
-         if (__builtin_expect (tzspec == NULL, 0))
+         if (__glibc_unlikely (tzspec == NULL))
            {
            use_last:
              i = num_transitions;
@@ -681,7 +692,7 @@ __tzfile_compute (time_t timer, int use_localtime,
 
          /* Convert to broken down structure.  If this fails do not
             use the string.  */
-         if (__builtin_expect (! __offtime (&timer, 0, tp), 0))
+         if (__glibc_unlikely (! __offtime (&timer, 0, tp)))
            goto use_last;
 
          /* Use the rules from the TZ string to compute the change.  */
@@ -690,7 +701,7 @@ __tzfile_compute (time_t timer, int use_localtime,
          /* 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 (__builtin_expect (zone_names == (char *) &leaps[num_leaps], 0))
+         if (__glibc_unlikely (zone_names == (char *) &leaps[num_leaps]))
            {
              assert (num_types == 2);
              __tzname[0] = __tzstring (zone_names);
@@ -772,7 +783,7 @@ __tzfile_compute (time_t timer, int use_localtime,
              ++j;
            }
 
-         if (__builtin_expect (__tzname[0] == NULL, 0))
+         if (__glibc_unlikely (__tzname[0] == NULL))
            __tzname[0] = __tzname[1];
 
          i = type_idxs[i - 1];