Subject: standards/21722: ISO-C conforming use of struct tm can crash
To: None <>
From: Richard Earnshaw <>
List: netbsd-bugs
Date: 05/30/2003 13:58:59
>Number:         21722
>Category:       standards
>Synopsis:       ISO-C conforming use of struct tm can crash
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    standards-manager
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Fri May 30 13:00:00 UTC 2003
>Originator:     Richard Earnshaw
>Release:        NetBSD 1.6R
System: NetBSD 1.6R NetBSD 1.6R (GENERIC) #1: Tue Apr 29 10:12:54 BST 2003 shark
Architecture: arm
Machine: shark

	A program that makes use of only the fields in struct tm that are
	specified in the ISO-C standards can crash inside of strftime because
	the library accesses non-standard fields.

	The following program sets all of the fields required by ISO-C and then
	calls strftime with a %Z template (an ISO-C documented option).

	Compile and run the following program with

	cc -std=c89 timetest.c

#include <stdio.h>
#include <time.h>

  struct tm a_time, *now;
  time_t curtime;
  char buf[1024];

  curtime = time(NULL);
  now = localtime(&curtime);
  /* Poison structure.  */
  memset (&a_time, 1, sizeof(struct tm));

  /* Now set all fields required by c89.  */

  a_time.tm_year = now->tm_year;
  a_time.tm_mon = now->tm_mon;
  a_time.tm_mday = now->tm_mday;
  a_time.tm_hour = now->tm_hour;
  a_time.tm_min = now->tm_min;
  a_time.tm_sec = now->tm_sec;
  a_time.tm_isdst = now->tm_isdst;
  a_time.tm_yday = now->tm_yday;
  a_time.tm_wday = now->tm_wday;

  /* Now print the timezone name.  */
  strftime (buf, sizeof(buf) - 1, "%Z", &a_time);

  printf("Z = %s\n", buf);
  return 0;

	There is a similar problem with %z (part of c99) and use of the 
	tm_gmtoff.  But that only causes a bogus result to be returned.

	strftime and other time functions must not depend on non-iso fields
	being set when performing actions documented by the standard.