uClibc: make UCLIBC_HAS_LONG_DOUBLE_MATH generic
[openwrt/svn-archive/archive.git] / toolchain / uClibc / patches-0.9.31 / 000-upstream-ctime_fix.patch
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
5
6 fixes issue2209:
7 ctime() was updated in 0.9.31 to call localtime_r() instead of
8 localtime() to
9 avoid using a static buffer. Unfortunately, this change replaces the
10 static
11 buffer (which is zeroed out on initialization) with an uninitialized
12 local
13 buffer.
14
15 In the common case, this has no effect. However, with a sufficiently
16 large
17 time_t value, the value returned differs from that returned by
18 asctime(localtime(t)), and thus violates the ANSI/ISO standard.
19
20 An example input is (on a 64-bit machine):
21 time_t t = 0x7ffffffffff6c600;
22
23 Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
24 ---
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:
31 */
32 struct tm xtm;
33 + memset(&xtm, 0, sizeof(xtm));
34
35 return asctime(localtime_r(t, &xtm));
36 }
37 diff --git a/test/time/tst-ctime.c b/test/time/tst-ctime.c
38 new file mode 100644
39 index 0000000..91d827a
40 --- a/dev/null
41 +++ b/test/time/tst-ctime.c
42 @@ -0,0 +1,44 @@
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.
47 + */
48 +
49 +#include <stdio.h>
50 +#include <stdlib.h>
51 +#include <string.h>
52 +#include <time.h>
53 +
54 +#define MAX_POSITIVE(type) (~0 & ~((type) 1 << (sizeof(type)*8 - 1)))
55 +
56 +int do_test(int argc, char **argv) {
57 + char *correct = 0, *s;
58 + int status;
59 +
60 + /* need a very high positive number (e.g., max - 1024) */
61 + time_t test = MAX_POSITIVE(time_t) - 1024;
62 +
63 + s = asctime(localtime(&test));
64 +
65 + if (s) {
66 + // copy static buffer to heap
67 + correct = malloc(strlen(s)+1);
68 + strcpy(correct, s);
69 + }
70 +
71 + s = ctime(&test);
72 +
73 + printf("ANSI:\t%suClibc:\t%s", correct, s);
74 +
75 + if (s != correct && strcmp(correct, s))
76 + status = EXIT_FAILURE;
77 + else
78 + status = EXIT_SUCCESS;
79 +
80 + if (correct)
81 + free(correct);
82 +
83 + return status;
84 +}
85 +
86 +#include <test-skeleton.c>