From: Joel Rosdahl Date: Sun, 22 Sep 2019 17:46:42 +0000 (+0200) Subject: Don’t crash if localtime returns null pointer in localtime_r replacement X-Git-Tag: v4.0~781 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=87550e9c38560fa28b8d36753de18f980811a421;p=thirdparty%2Fccache.git Don’t crash if localtime returns null pointer in localtime_r replacement This seems to happen in some unknown Windows environment, see #450. Fixes #450. (cherry picked from commit 8c4ffbab96a8f4bebc27b39ad3a235a5d8baeab3) --- diff --git a/src/hashutil.cpp b/src/hashutil.cpp index d3a997407..93317522c 100644 --- a/src/hashutil.cpp +++ b/src/hashutil.cpp @@ -88,13 +88,16 @@ hash_source_code_string(const Config& config, hash_string_buffer(hash, str, len); if (result & HASH_SOURCE_CODE_FOUND_DATE) { + cc_log("Found __DATE__ in %s", path); + // Make sure that the hash sum changes if the (potential) expansion of // __DATE__ changes. time_t t = time(NULL); struct tm now; - localtime_r(&t, &now); - cc_log("Found __DATE__ in %s", path); hash_delimiter(hash, "date"); + if (!localtime_r(&t, &now)) { + return HASH_SOURCE_CODE_ERROR; + } hash_int(hash, now.tm_year); hash_int(hash, now.tm_mon); hash_int(hash, now.tm_mday); diff --git a/src/util.cpp b/src/util.cpp index 978cd227e..7948938ba 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -1134,8 +1134,13 @@ struct tm* localtime_r(const time_t* timep, struct tm* result) { struct tm* tm = localtime(timep); - *result = *tm; - return result; + if (tm) { + *result = *tm; + return result; + } else { + memset(result, 0, sizeof(*result)); + return NULL; + } } #endif