From 8c4ffbab96a8f4bebc27b39ad3a235a5d8baeab3 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. --- src/hashutil.c | 7 +++++-- src/util.c | 10 ++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/hashutil.c b/src/hashutil.c index edf446c4c..482ce6b7f 100644 --- a/src/hashutil.c +++ b/src/hashutil.c @@ -104,13 +104,16 @@ hash_source_code_string( 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.c b/src/util.c index 8f4490ffc..2dbe6652c 100644 --- a/src/util.c +++ b/src/util.c @@ -1224,8 +1224,14 @@ 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.2