1 From f6651fa449e1d4bbbb466b091f34e6752f6506f9 Mon Sep 17 00:00:00 2001
2 From: David A Ramos <daramos@gustav.stanford.edu>
3 Date: Tue, 27 Jul 2010 11:10:15 +0000
4 Subject: Fix ctime() standard compliance bug
7 ctime() was updated in 0.9.31 to call localtime_r() instead of
9 avoid using a static buffer. Unfortunately, this change replaces the
11 buffer (which is zeroed out on initialization) with an uninitialized
15 In the common case, this has no effect. However, with a sufficiently
17 time_t value, the value returned differs from that returned by
18 asctime(localtime(t)), and thus violates the ANSI/ISO standard.
20 An example input is (on a 64-bit machine):
21 time_t t = 0x7ffffffffff6c600;
23 Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
25 diff --git a/libc/misc/time/time.c b/libc/misc/time/time.c
26 index dfa8c0d..0d12bf3 100644
27 --- a/libc/misc/time/time.c
28 +++ b/libc/misc/time/time.c
29 @@ -479,6 +479,7 @@ char *ctime(const time_t *t)
30 * localtime's static buffer:
33 + memset(&xtm, 0, sizeof(xtm));
35 return asctime(localtime_r(t, &xtm));
37 diff --git a/test/time/tst-ctime.c b/test/time/tst-ctime.c
39 index 0000000..91d827a
41 +++ b/test/time/tst-ctime.c
43 +/* vi: set sw=4 ts=4: */
44 +/* testcase for ctime(3) with large time
45 + * Copyright (C) 2010 David A Ramos <daramos@gustav.stanford.edu>
46 + * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
54 +#define MAX_POSITIVE(type) (~0 & ~((type) 1 << (sizeof(type)*8 - 1)))
56 +int do_test(int argc, char **argv) {
57 + char *correct = 0, *s;
60 + /* need a very high positive number (e.g., max - 1024) */
61 + time_t test = MAX_POSITIVE(time_t) - 1024;
63 + s = asctime(localtime(&test));
66 + // copy static buffer to heap
67 + correct = malloc(strlen(s)+1);
73 + printf("ANSI:\t%suClibc:\t%s", correct, s);
75 + if (s != correct && strcmp(correct, s))
76 + status = EXIT_FAILURE;
78 + status = EXIT_SUCCESS;
86 +#include <test-skeleton.c>