]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Don’t crash if localtime returns null pointer in localtime_r replacement
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 22 Sep 2019 17:46:42 +0000 (19:46 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 22 Sep 2019 17:46:42 +0000 (19:46 +0200)
This seems to happen in some unknown Windows environment, see #450.

Fixes #450.

src/hashutil.c
src/util.c

index edf446c4cbef8964029b651daf9f83657c6f1f0e..482ce6b7f074af9181eea6e85cbc22217a0e7f5b 100644 (file)
@@ -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);
index 8f4490ffc46df80c20f8d430a6559d492a089308..2dbe6652cc6d97b8446cd73a5c2c9ac2d7f774ac 100644 (file)
@@ -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