]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Check for word boundaries when scanning for __DATE__/__TIME__
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 13 Jan 2019 20:13:09 +0000 (21:13 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 13 Jan 2019 20:13:09 +0000 (21:13 +0100)
As suggested in #347.

doc/NEWS.adoc
src/hashutil.c
unittest/test_hashutil.c

index 9e129df0257a6c60fe236e0da7bdc0245e998d61..07e7de6610698a58f26d9ce9f0853be15b897193 100644 (file)
@@ -45,6 +45,10 @@ Changes
 - The GCC variables “DEPENDENCIES_OUTPUT” and “SUNPRO_DEPENDENCIES” are now
   supported correctly.
 
+- The algorithm that scans for `__DATE_` and `__TIME__` tokens in the hashed
+  source code now doesn't produce false positives for tokens where `__DATE__`
+  or `__TIME__` is a substring.
+
 
 ccache 3.5.1
 ------------
index 4ed10d774d2a8d425811fb4960d22db50019e223..789bf437b51efcffe869a5eb67e89f540ba9aa7e 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2009-2018 Joel Rosdahl
+// Copyright (C) 2009-2019 Joel Rosdahl
 //
 // This program is free software; you can redistribute it and/or modify it
 // under the terms of the GNU General Public License as published by the Free
@@ -62,18 +62,18 @@ check_for_temporal_macros(const char *str, size_t len)
                // Check whether the substring ending at str[i] has the form "__...E__". On
                // the assumption that 'E' is less common in source than '_', we check
                // str[i-2] first.
-               if (str[i - 2] == 'E' &&
-                   str[i - 0] == '_' &&
-                   str[i - 7] == '_' &&
-                   str[i - 1] == '_' &&
-                   str[i - 6] == '_') {
+               if (str[i - 2] == 'E'
+                   && str[i - 0] == '_'
+                   && str[i - 7] == '_'
+                   && str[i - 1] == '_'
+                   && str[i - 6] == '_'
+                   && (i < 8 || (str[i - 8] != '_' && !isalnum(str[i - 8])))
+                   && (i + 1 >= len || (str[i + 1] != '_' && !isalnum(str[i + 1])))) {
                        // Check the remaining characters to see if the substring is "__DATE__"
                        // or "__TIME__".
-                       if (str[i - 5] == 'D' && str[i - 4] == 'A' &&
-                           str[i - 3] == 'T') {
+                       if (str[i - 5] == 'D' && str[i - 4] == 'A' && str[i - 3] == 'T') {
                                result |= HASH_SOURCE_CODE_FOUND_DATE;
-                       } else if (str[i - 5] == 'T' && str[i - 4] == 'I' &&
-                                  str[i - 3] == 'M') {
+                       } else if (str[i - 5] == 'T' && str[i - 4] == 'I' && str[i - 3] == 'M') {
                                result |= HASH_SOURCE_CODE_FOUND_TIME;
                        }
                }
index 2b2dbc9b1e488bb6b1c8153a37f1e393de93c45c..d05769e5f69f7ce61dde74c227cca0f485d8ff51 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2010-2018 Joel Rosdahl
+// Copyright (C) 2010-2019 Joel Rosdahl
 //
 // This program is free software; you can redistribute it and/or modify it
 // under the terms of the GNU General Public License as published by the Free
@@ -149,6 +149,13 @@ TEST(check_for_temporal_macros)
                "#define ab __DATE__";
 
        const char no_temporal[] =
+               "#define ab a__DATE__\n"
+               "#define ab  __DATE__a\n"
+               "#define ab A__DATE__\n"
+               "#define ab  __DATE__A\n"
+               "#define ab 0__DATE__\n"
+               "#define ab  __DATE__0\n"
+               "#define ab _ _DATE__\n"
                "#define ab _ _DATE__\n"
                "#define ab __ DATE__\n"
                "#define ab __D ATE__\n"