Hi Joel,

I've encountered a bug while playing with ccache: temporal macros are not detected correctly.

Patch attached.

Andrew
>From ada18fd6326c54fdb08ec1f66c923b9c601aa00e Mon Sep 17 00:00:00 2001
From: Andrew Stubbs <[email protected]>
Date: Mon, 8 Oct 2012 13:14:40 +0100
Subject: [PATCH] Detect __DATE__ and __TIME__ correctly.

The code to detect __DATE__ and __TIME__ was off-by-one, and therefore
totally failed to detect time macros unless by chance alignments (1 in eight
macros might be correctly aligned).

The problem is that the code expects that 'i' will point to the last
underscore, and the skip table expects 'i' to point to the point after
the end of the string. For example, if str[i] == 'E' then the skip table
moves 'i' on 3 bytes, whereas the code only works with a 2-byte skip.

I've corrected the problem by adjusting the code to match the table.

I'm not completely sure why the tests did not fail, but they still pass.

Signed-off-by: Andrew Stubbs <[email protected]>
---
 hashutil.c |   20 ++++++++++----------
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/hashutil.c b/hashutil.c
index a15f251..530c087 100644
--- a/hashutil.c
+++ b/hashutil.c
@@ -60,9 +60,9 @@ check_for_temporal_macros(const char *str, size_t len)
 	/*
 	 * 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.
+	 * starts at 8.
 	 */
-	size_t i = 7;
+	size_t i = 8;
 
 	while (i < len) {
 		/*
@@ -70,21 +70,21 @@ check_for_temporal_macros(const char *str, size_t len)
 		 * 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] == '_' &&
+		if (str[i - 3] == 'E' &&
 		    str[i - 1] == '_' &&
-		    str[i - 6] == '_') {
+		    str[i - 8] == '_' &&
+		    str[i - 2] == '_' &&
+		    str[i - 7] == '_') {
 			/*
 			 * 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 - 6] == 'D' && str[i - 5] == 'A' &&
+			    str[i - 4] == '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 - 6] == 'T' && str[i - 5] == 'I' &&
+				 str[i - 4] == 'M') {
 				result |= HASH_SOURCE_CODE_FOUND_TIME;
 			}
 		}
-- 
1.7.9.5

_______________________________________________
ccache mailing list
[email protected]
https://lists.samba.org/mailman/listinfo/ccache

Reply via email to