]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Correctly handle backslash in strings in hash_source_code_string
authorJoel Rosdahl <joel@rosdahl.net>
Thu, 31 May 2012 20:38:49 +0000 (22:38 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Thu, 31 May 2012 20:38:49 +0000 (22:38 +0200)
hashutil.c
test/test_hashutil.c

index d69e629fdd115497340d976558701a1bebc66df1..6cc9e6cb7adc4a5f417f39c7e4a354e95e7a267e 100644 (file)
@@ -69,6 +69,7 @@ hash_source_code_string(
        size_t hashbuflen = 0;
        int result = HASH_SOURCE_CODE_OK;
        extern unsigned sloppiness;
+       bool seen_backslash;
 
        p = str;
        end = str + len;
@@ -115,7 +116,18 @@ hash_source_code_string(
                case '"':
                        HASH(*p);
                        p++;
-                       while (p < end && (*p != '"' || *(p-1) == '\\')) {
+                       seen_backslash = false;
+                       while (p < end) {
+                               if (seen_backslash) {
+                                       seen_backslash = false;
+                               } else if (*p == '"') {
+                                       /* Found end of string. */
+                                       HASH(*p);
+                                       p++;
+                                       break;
+                               } else if (*p == '\\') {
+                                       seen_backslash = true;
+                               }
                                HASH(*p);
                                p++;
                        }
index 225841a5ad847ce6c38abcf737e39f90d4bec7a4..4a72d8491304dcf50e44d5b0b57e68933d845302 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010 Joel Rosdahl
+ * Copyright (C) 2010, 2012 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
@@ -99,4 +99,93 @@ TEST(hash_multicommand_output_error_handling)
        CHECK(!hash_multicommand_output(&h2, "false; true", "not used"));
 }
 
+TEST(hash_source_code_simple_case)
+{
+       struct mdfour h;
+       char input[] = "abc";
+       size_t input_len = strlen(input);
+       hash_start(&h);
+       hash_source_code_string(&h, input, input_len, "");
+       CHECK_STR_EQ_FREE2("a448017aaf21d8525fc10ae87aa6729d-3", hash_result(&h));
+}
+
+TEST(hash_source_code_with_c_style_comment)
+{
+       struct mdfour h;
+       char input[] = "a/*b*/c";
+       size_t input_len = strlen(input);
+
+       hash_start(&h);
+       hash_source_code_string(&h, input, input_len, "");
+       CHECK_STR_EQ_FREE2("1c2c87080ee03418fb1279e3b1f09a68-3", hash_result(&h));
+
+       input[3] = 'd';
+       hash_start(&h);
+       hash_source_code_string(&h, input, input_len, "");
+       CHECK_STR_EQ_FREE2("1c2c87080ee03418fb1279e3b1f09a68-3", hash_result(&h));
+}
+
+TEST(hash_source_code_with_cplusplus_style_comment)
+{
+       struct mdfour h;
+       char input[] = "a//b\nc";
+       size_t input_len = strlen(input);
+
+       hash_start(&h);
+       hash_source_code_string(&h, input, input_len, "");
+       CHECK_STR_EQ_FREE2("4a3fbbe3c140fa193227dba3814db6e6-3", hash_result(&h));
+
+       input[3] = 'd';
+       hash_start(&h);
+       hash_source_code_string(&h, input, input_len, "");
+       CHECK_STR_EQ_FREE2("4a3fbbe3c140fa193227dba3814db6e6-3", hash_result(&h));
+}
+
+TEST(hash_source_code_with_comment_inside_string)
+{
+       struct mdfour h;
+       char input[] = "a\"//b\"c";
+       size_t input_len = strlen(input);
+
+       hash_start(&h);
+       hash_source_code_string(&h, input, input_len, "");
+       CHECK_STR_EQ_FREE2("4c2fa74b0843d8f93df5c04c98ccb0a4-7", hash_result(&h));
+
+       input[4] = 'd';
+       hash_start(&h);
+       hash_source_code_string(&h, input, input_len, "");
+       CHECK_STR_EQ_FREE2("f0069218ec640008cbfa2d150c1061bb-7", hash_result(&h));
+}
+
+TEST(hash_source_code_with_quote_in_string)
+{
+       struct mdfour h;
+       char input[] = "a\"\\\"b//c\""; // a"\"b//c"
+       size_t input_len = strlen(input);
+
+       hash_start(&h);
+       hash_source_code_string(&h, input, input_len, "");
+       CHECK_STR_EQ_FREE2("c4e45e7a7f6f29b000a51f187dc4cf06-9", hash_result(&h));
+
+       hash_start(&h);
+       input[7] = 'd';
+       hash_source_code_string(&h, input, input_len, "");
+       CHECK_STR_EQ_FREE2("bef8fb852dddcee189b91b068a621c55-9", hash_result(&h));
+}
+
+TEST(hash_source_code_with_backslash_at_string_end)
+{
+       struct mdfour h;
+       char input[] = "a\"\\\\\"b//c"; // a"\\"b//c
+       size_t input_len = strlen(input);
+
+       hash_start(&h);
+       hash_source_code_string(&h, input, input_len, "");
+       CHECK_STR_EQ_FREE2("7f3ccf27edadad1b90cb2cffb59775d6-6", hash_result(&h));
+
+       input[input_len - 1] = 'd';
+       hash_source_code_string(&h, input, input_len, "");
+       CHECK_STR_EQ_FREE2("7f3ccf27edadad1b90cb2cffb59775d6-6", hash_result(&h));
+}
+
 TEST_SUITE_END