Improve robustness of asctime() and asctime_r().

* Return NULL and set EINVAL if the given tm pointer is NULL. This 
  isn't mandated by the POSIX base specs, but it just makes sense.
This commit is contained in:
Oliver Tappe 2013-06-23 17:01:12 +02:00
parent 51bce887cd
commit ece582547a

View File

@ -8,6 +8,8 @@
#include <time.h>
#include <stdio.h>
#include <errno_private.h>
#include "PosixLCTimeInfo.h"
@ -30,6 +32,11 @@ print_time(char* buffer, size_t bufferSize, const struct tm* tm)
extern "C" char*
asctime(const struct tm* tm)
{
if (tm == NULL) {
__set_errno(EINVAL);
return NULL;
}
static char buffer[26];
// That's enough to hold normal dates (i.e. with 4-digit years), for any
// other dates the behaviour of asctime() is undefined according to the
@ -42,6 +49,11 @@ asctime(const struct tm* tm)
extern "C" char*
asctime_r(const struct tm* tm, char* buffer)
{
if (tm == NULL) {
__set_errno(EINVAL);
return NULL;
}
return print_time(buffer, 26, tm);
// 26 bytes seems to be required by the standard, so we can't write more
}