From ece582547a43df2c2009900ac638fecb97c71d9c Mon Sep 17 00:00:00 2001 From: Oliver Tappe Date: Sun, 23 Jun 2013 17:01:12 +0200 Subject: [PATCH] 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. --- src/system/libroot/posix/time/asctime.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/system/libroot/posix/time/asctime.cpp b/src/system/libroot/posix/time/asctime.cpp index e4c292915a..c4c8ce69a4 100644 --- a/src/system/libroot/posix/time/asctime.cpp +++ b/src/system/libroot/posix/time/asctime.cpp @@ -8,6 +8,8 @@ #include #include +#include + #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 }