Attention to bug 170444

Thomas Bushnell BSG tb at becket.net
Sun Aug 28 19:47:58 EDT 2005


Thomas Bushnell BSG <tb at becket.net> writes:

> Indeed, the following program generates a segv with glibc:
>
>
> #include <time.h>
> #include <stdio.h>
>
> main ()
> {
>   char buffer[1000];
>   struct tm tm_str;
>
>   tm_str.tm_mday = 28;
>   tm_str.tm_mon = 7;
>   tm_str.tm_year = 105;
>   tm_str.tm_hour = 0;
>   tm_str.tm_min = 0;
>   tm_str.tm_sec = 0;
>   tm_str.tm_isdst = -1;
>     
>   strftime (buffer, 1000, "%A, %d %B, %Y", &tm_str);
>   printf ("%s\n", buffer);
> }

Sorry for all the email.  Turns out, not a glibc bug.

The problem here is that tm_wday is not initialized, and some locales
print the weekday as part of the default date format.  Whew.

So the correct fix is to use mktime to generate the day of week data,
with the enclosed patch.

Please also put on a todo list somewhere to fix all the uses of
strftime with locale-dependent format strings, first to deal with
return values of zero, and second to use mktime to fill in the
complete tm structure.

--- gnucash-1.8.10.orig/src/engine/date.c
+++ gnucash-1.8.10/src/engine/date.c
@@ -362,19 +362,25 @@
       sprintf (buff, "%2d.%2d.%-4d", day, month, year);
       break;
     case DATE_FORMAT_ISO:
+  iso_date:
       sprintf (buff, "%04d-%02d-%02d", year, month, day);
       break;
     case DATE_FORMAT_LOCALE:
       {
         struct tm tm_str;
+	time_t t;
+	int n;
 
         tm_str.tm_mday = day;
         tm_str.tm_mon = month - 1;    /* tm_mon = 0 through 11 */
         tm_str.tm_year = year - 1900; /* this is what the standard 
                                        * says, it's not a Y2K thing */
-
 	gnc_tm_set_day_start (&tm_str);
-        strftime (buff, MAX_DATE_LENGTH, GNC_D_FMT, &tm_str);
+	t = mktime (&tm_str);
+	localtime_r (&t, &tm_str);
+        n = strftime (buff, MAX_DATE_LENGTH, GNC_D_FMT, &tm_str);
+	if (n == 0)
+	  goto iso_date;
       }
       break;
 


More information about the gnucash-devel mailing list