]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
fix: Fix edge case where a non-temporal identifier is misidentified (#1227)
authorErik Flodin <erik@flodin.me>
Sun, 27 Nov 2022 20:32:36 +0000 (21:32 +0100)
committerGitHub <noreply@github.com>
Sun, 27 Nov 2022 20:32:36 +0000 (21:32 +0100)
If a non-temporal identifier, that ends with a temporal macro, happens
to be at the end of the buffer with the temporal suffix starting on the
avx boundary, then it would be incorrectly classified as a temporal
macro. This since the helper function lacks the context to see that the
data before the match is something that invalidates the match.

src/hashutil.cpp
unittest/test_hashutil.cpp

index 8d7151d680f25b42b968cf2cce1468775cf342ac..796eb3a2dcffd0c2a2d417f6bc8afd970ec652c7 100644 (file)
@@ -93,14 +93,14 @@ check_for_temporal_macros_helper(std::string_view str, size_t pos)
 }
 
 int
-check_for_temporal_macros_bmh(std::string_view str)
+check_for_temporal_macros_bmh(std::string_view str, size_t start = 0)
 {
   int result = 0;
 
   // We're using the Boyer-Moore-Horspool algorithm, which searches starting
   // from the *end* of the needle. Our needles are 8 characters long, so i
   // starts at 7.
-  size_t i = 7;
+  size_t i = start + 7;
 
   while (i < str.length()) {
     // Check whether the substring ending at str[i] has the form "_....E..". On
@@ -173,7 +173,7 @@ check_for_temporal_macros_avx2(std::string_view str)
     }
   }
 
-  result |= check_for_temporal_macros_bmh(str.substr(pos));
+  result |= check_for_temporal_macros_bmh(str, pos);
 
   return result;
 }
index e6f62627c210125d3be2969d7e77e6592d1523e3..84199d16403084ef43869d28780b4ae9b4342da5 100644 (file)
@@ -167,6 +167,10 @@ TEST_CASE("check_for_temporal_macros")
     "#define alphabet abcdefghijklmnopqrstuvwxyz\n"
     "__DATE__";
 
+  const std::string_view no_temporal_at_avx_boundary =
+    "#define alphabet abcdefghijklmnopqrstuvwxyz\n"
+    "a__DATE__";
+
   CHECK(check_for_temporal_macros(time_start));
   CHECK(!check_for_temporal_macros(time_start.substr(1)));
 
@@ -226,9 +230,12 @@ TEST_CASE("check_for_temporal_macros")
   CHECK(!check_for_temporal_macros(no_temporal.substr(6)));
   CHECK(!check_for_temporal_macros(no_temporal.substr(7)));
 
-  for (size_t i = 0; i < sizeof(temporal_at_avx_boundary) - 8; ++i) {
+  for (size_t i = 0; i < temporal_at_avx_boundary.size() - 8; ++i) {
     CHECK(check_for_temporal_macros(temporal_at_avx_boundary.substr(i)));
   }
+  for (size_t i = 0; i < no_temporal_at_avx_boundary.size() - 8; ++i) {
+    CHECK(!check_for_temporal_macros(no_temporal_at_avx_boundary.substr(i)));
+  }
 }
 
 TEST_SUITE_END();