mirror of
https://git.busybox.net/uClibc
synced 2025-05-08 23:02:28 +08:00
Fix ctime() standard compliance bug
fixes issue2209: 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; Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
This commit is contained in:
parent
d4ede2b0a4
commit
f6651fa449
@ -479,6 +479,7 @@ char *ctime(const time_t *t)
|
||||
* localtime's static buffer:
|
||||
*/
|
||||
struct tm xtm;
|
||||
memset(&xtm, 0, sizeof(xtm));
|
||||
|
||||
return asctime(localtime_r(t, &xtm));
|
||||
}
|
||||
|
44
test/time/tst-ctime.c
Normal file
44
test/time/tst-ctime.c
Normal file
@ -0,0 +1,44 @@
|
||||
/* vi: set sw=4 ts=4: */
|
||||
/* testcase for ctime(3) with large time
|
||||
* Copyright (C) 2010 David A Ramos <daramos@gustav.stanford.edu>
|
||||
* Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#define MAX_POSITIVE(type) (~0 & ~((type) 1 << (sizeof(type)*8 - 1)))
|
||||
|
||||
int do_test(int argc, char **argv) {
|
||||
char *correct = 0, *s;
|
||||
int status;
|
||||
|
||||
/* need a very high positive number (e.g., max - 1024) */
|
||||
time_t test = MAX_POSITIVE(time_t) - 1024;
|
||||
|
||||
s = asctime(localtime(&test));
|
||||
|
||||
if (s) {
|
||||
// copy static buffer to heap
|
||||
correct = malloc(strlen(s)+1);
|
||||
strcpy(correct, s);
|
||||
}
|
||||
|
||||
s = ctime(&test);
|
||||
|
||||
printf("ANSI:\t%suClibc:\t%s", correct, s);
|
||||
|
||||
if (s != correct && strcmp(correct, s))
|
||||
status = EXIT_FAILURE;
|
||||
else
|
||||
status = EXIT_SUCCESS;
|
||||
|
||||
if (correct)
|
||||
free(correct);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
#include <test-skeleton.c>
|
Loading…
x
Reference in New Issue
Block a user