From 87550e9c38560fa28b8d36753de18f980811a421 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Sun, 22 Sep 2019 19:46:42 +0200 Subject: [PATCH] =?utf8?q?Don=E2=80=99t=20crash=20if=20localtime=20returns?= =?utf8?q?=20null=20pointer=20in=20localtime=5Fr=20replacement?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This seems to happen in some unknown Windows environment, see #450. Fixes #450. (cherry picked from commit 8c4ffbab96a8f4bebc27b39ad3a235a5d8baeab3) --- src/hashutil.cpp | 7 +++++-- src/util.cpp | 9 +++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) 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 -- 2.47.3