Bug 2209

Summary: ctime(*t) can return garbage with large t
Product: uClibc Reporter: David Ramos <ramos>
Component: Standard ComplianceAssignee: unassigned
Status: RESOLVED FIXED    
Severity: minor CC: ramos, uclibc-cvs
Priority: P5    
Version: 0.9.31   
Target Milestone: ---   
Hardware: PC   
OS: Linux   
Host: Target:
Build:
Attachments: Patch containing testcase and proposed fix

Description David Ramos 2010-07-12 07:04:52 UTC
ctime() was updated in 0.9.31 to call localtime_r() instead of localtime() to avoid using a static buffer. Unfortunately, this change replaces the static buffer (which is zeroed out on initialization) with an uninitialized local buffer.

In the common case, this has no effect. However, with a sufficiently large time_t value, the value returned differs from that returned by asctime(localtime(t)), and thus violates the ANSI/ISO standard.

An example input is (on a 64-bit machine):
time_t t = 0x7ffffffffff6c600;

When passed to uClibc 0.9.30.3, the resulting string is:
"Sun Jan  0 15:32:16 1900"

When passed to uClibc 0.9.31, the resulting string is:
"??? ??? ?? 15:32:16 ????"

The recommended fix is to zero-out the 'tm' struct using memset() before calling localtime_r.
Comment 1 Bernhard Reutner-Fischer 2010-07-14 20:20:11 UTC
(In reply to comment #0)

> The recommended fix is to zero-out the 'tm' struct using memset() before
> calling localtime_r.

yes. Perhaps you could provide a testsuite entry for this one (test/time/tst-futimens1.c is a good example for a test)? That'd be awesome..
Comment 2 David Ramos 2010-07-14 23:01:23 UTC
Created attachment 2239 [details]
Patch containing testcase and proposed fix
Comment 3 Bernhard Reutner-Fischer 2010-07-28 07:36:56 UTC
Applied as 85b7ea33088fa16d319adbd228fc152f5b9da730. Thanks!