-/* 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
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 ();
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;
}
&& 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. */
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;
__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;
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')
> (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
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
|| 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
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))
{
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)
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);
}
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;
}
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;
}
{
/* 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
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;
+ 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. */
__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;
}
__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
__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
++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;
/* 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. */
/* 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);
++j;
}
- if (__builtin_expect (__tzname[0] == NULL, 0))
+ if (__glibc_unlikely (__tzname[0] == NULL))
__tzname[0] = __tzname[1];
i = type_idxs[i - 1];