This is the mail archive of the ecos-patches@sourceware.org mailing list for the eCos project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

strptime() clobbers its return values


strptime() initializes the contents of its "struct tm" return structure at the beginning of the function. However, several of the strptime() format specifiers result in recursive calls to strptime() which then clobber any values which were parsed earlier in the format string. In particular, the following format specifiers call strptime() recursively: %D, %r, %R, %T, %X, %x.

For example, suppose you wanted to parse a string like "2006:06:13 12:22:01". If you used the format specified "%x %X", the "%x" (i.e. date) portion of the tm structure will get clobbered with the init values during the %X (i.e. time) processing.

The proposed fix removes the initialization code. POSIX is somewhat ambivalent about whether struct tm should be initialized:

   It is unspecified whether multiple calls to strptime() using the
   same tm structure will update the current contents of the structure
   or overwrite all contents of the structure. Conforming applications
   should make a single call to strptime() with a format and all data
   needed to completely specify the date and time being converted.

The GNU/Linux man page offers this practical opinion:

   In  principle,  this  function does not initialize tm but only
   stores the values specified.  This means that tm should be
   initialized before the call.  Details differ a bit between different
   Unix systems.  The GNU libc implementation does not touch those
   fields which are not explicitly specified, except that it recomputes
   the tm_wday and tm_yday field if any of the year, month, or day
   elements changed.


-- Dan Jakubiec Systech Corporation

Index: ecos/packages/language/c/libc/time/current/src/strptime.cxx
===================================================================
RCS file: /cvs/ecos/ecos/packages/language/c/libc/time/current/src/strptime.cxx,v
retrieving revision 1.5
diff -u -5 -p -r1.5 strptime.cxx
--- ecos/packages/language/c/libc/time/current/src/strptime.cxx	8 Aug 2004 22:39:25 -0000	1.5
+++ ecos/packages/language/c/libc/time/current/src/strptime.cxx	13 Jun 2006 17:22:36 -0000
@@ -184,21 +184,10 @@ set_week_number_mon4 (struct tm *timeptr
 char *
 strptime (const char *buf, const char *format, struct tm *timeptr)
 {
     char c;
 
-    timeptr->tm_yday = 1;  // Initialize to a well known, valid date
-    timeptr->tm_isdst = 0; //   Tuesday March 18 14:05:00 2003 UTC
-    timeptr->tm_sec = 0;
-    timeptr->tm_min = 5;
-    timeptr->tm_hour = 14;
-    timeptr->tm_mday = 18;
-    timeptr->tm_mon = 2;
-    timeptr->tm_year = 103;
-    timeptr->tm_wday = 2;
-    timeptr->tm_yday = 77;
-
     for (; (c = *format) != '\0'; ++format) {
 	char *s;
 	int ret;
 
 	if (isspace (c)) {

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]