path: root/src/time/__tz.c
diff options
authorRich Felker <>2013-08-24 12:59:02 -0400
committerRich Felker <>2013-08-24 12:59:02 -0400
commitd78be392e144c338f58ce6a51d82c859126c137d (patch)
tree154cc7c69af95a812cfb2d3dab8622e6f9bd5a77 /src/time/__tz.c
parent0f9b1f672b68b7c3570f07b130cc5c8938b22bad (diff)
fix strftime handling of time zone data
this may need further revision in the future, since POSIX is rather unclear on the requirements, and is designed around the assumption of POSIX TZ specifiers which are not sufficiently powerful to represent real-world timezones (this is why zoneinfo support was added). the basic issue is that strftime gets the string and numeric offset for the timezone from the extra fields in struct tm, which are initialized when calling localtime/gmtime/etc. however, a conforming application might have created its own struct tm without initializing these fields, in which case using __tm_zone (a pointer) could crash. other zoneinfo-based implementations simply check for a null pointer, but otherwise can still crash of the field contains junk. simply ignoring __tm_zone and using tzname[] would "work" but would give incorrect results in time zones with more complex rules. I feel like this would lower the quality of implementation. instead, simply validate __tm_zone: unless it points to one of the zone name strings managed by the timezone system, assume it's invalid. this commit also fixes several other minor bugs with formatting: tm_isdst being negative is required to suppress printing of the zone formats, and %z was using the wrong format specifiers since the type of val was changed, resulting in bogus output.
Diffstat (limited to 'src/time/__tz.c')
1 files changed, 17 insertions, 3 deletions
diff --git a/src/time/__tz.c b/src/time/__tz.c
index 36b59802..dfeac519 100644
--- a/src/time/__tz.c
+++ b/src/time/__tz.c
@@ -15,11 +15,12 @@ weak_alias(__tzname, tzname);
static char std_name[TZNAME_MAX+1];
static char dst_name[TZNAME_MAX+1];
+const char __gmt[] = "GMT";
static int dst_off;
static int r0[5], r1[5];
-static const unsigned char *zi, *trans, *index, *types, *abbrevs;
+static const unsigned char *zi, *trans, *index, *types, *abbrevs, *abbrevs_end;
static size_t map_size;
static char old_tz_buf[32];
@@ -127,7 +128,7 @@ static void do_tzset()
s = getenv("TZ");
- if (!s || !*s) s = "GMT0";
+ if (!s || !*s) s = __gmt;
if (old_tz && !strcmp(s, old_tz)) return;
@@ -184,6 +185,7 @@ static void do_tzset()
index = trans + (zi_read32(trans-12) << scale);
types = index + zi_read32(trans-12);
abbrevs = types + 6*zi_read32(trans-8);
+ abbrevs_end = abbrevs + zi_read32(trans-4);
if (zi[map_size-1] == '\n') {
for (s = (const char *)zi+map_size-2; *s!='\n'; s--);
@@ -192,7 +194,7 @@ static void do_tzset()
- if (!s) s = "GMT0";
+ if (!s) s = __gmt;
getname(std_name, &s);
__tzname[0] = std_name;
__timezone = getoff(&s);
@@ -387,3 +389,15 @@ void __tzset()
weak_alias(__tzset, tzset);
+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]
+ && (uintptr_t)p-(uintptr_t)abbrevs >= abbrevs_end - abbrevs)
+ p = "";
+ UNLOCK(lock);
+ return p;