From: Joel Rosdahl Date: Sun, 13 Jan 2019 20:13:09 +0000 (+0100) Subject: Check for word boundaries when scanning for __DATE__/__TIME__ X-Git-Tag: v3.6~7 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8602e1b19fd8f47943c8fb8cd3d795b3d8ea52c9;p=thirdparty%2Fccache.git Check for word boundaries when scanning for __DATE__/__TIME__ As suggested in #347. --- diff --git a/doc/NEWS.adoc b/doc/NEWS.adoc index 9e129df02..07e7de661 100644 --- a/doc/NEWS.adoc +++ b/doc/NEWS.adoc @@ -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 ------------ diff --git a/src/hashutil.c b/src/hashutil.c index 4ed10d774..789bf437b 100644 --- a/src/hashutil.c +++ b/src/hashutil.c @@ -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; } } diff --git a/unittest/test_hashutil.c b/unittest/test_hashutil.c index 2b2dbc9b1..d05769e5f 100644 --- a/unittest/test_hashutil.c +++ b/unittest/test_hashutil.c @@ -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"