os/time: fix os_localtime_r on win + add func_attr

- it makes no sense for these functions to take NULL pointers
- if `localtime()` on Windows returns a NULL pointer, the old code would try
  to dereference it.
This commit is contained in:
Nicolas Hillegeer 2014-07-20 16:53:18 +02:00 committed by Justin M. Keyes
parent 6610b002ef
commit 636ddd075c

View File

@ -84,7 +84,8 @@ static void microdelay(uint64_t microseconds)
/// Portable version of POSIX localtime_r() /// Portable version of POSIX localtime_r()
/// ///
/// @return NULL in case of error /// @return NULL in case of error
struct tm *os_localtime_r(const time_t *clock, struct tm *result) struct tm *os_localtime_r(const time_t *restrict clock,
struct tm *restrict result) FUNC_ATTR_NONNULL_ALL
{ {
#ifdef UNIX #ifdef UNIX
// POSIX provides localtime_r() as a thread-safe version of localtime(). // POSIX provides localtime_r() as a thread-safe version of localtime().
@ -93,6 +94,9 @@ struct tm *os_localtime_r(const time_t *clock, struct tm *result)
// Windows version of localtime() is thread-safe. // Windows version of localtime() is thread-safe.
// See http://msdn.microsoft.com/en-us/library/bf12f0hc%28VS.80%29.aspx // See http://msdn.microsoft.com/en-us/library/bf12f0hc%28VS.80%29.aspx
struct tm *local_time = localtime(clock); // NOLINT struct tm *local_time = localtime(clock); // NOLINT
if (!local_time) {
return NULL;
}
*result = *local_time; *result = *local_time;
return result; return result;
#endif #endif
@ -103,7 +107,7 @@ return result;
/// @param result Pointer to a 'struct tm' where the result should be placed /// @param result Pointer to a 'struct tm' where the result should be placed
/// @return A pointer to a 'struct tm' in the current time zone (the 'result' /// @return A pointer to a 'struct tm' in the current time zone (the 'result'
/// argument) or NULL in case of error /// argument) or NULL in case of error
struct tm *os_get_localtime(struct tm *result) struct tm *os_get_localtime(struct tm *result) FUNC_ATTR_NONNULL_ALL
{ {
time_t rawtime = time(NULL); time_t rawtime = time(NULL);
return os_localtime_r(&rawtime, result); return os_localtime_r(&rawtime, result);