From: Joel Rosdahl Date: Thu, 31 May 2012 20:38:49 +0000 (+0200) Subject: Correctly handle backslash in strings in hash_source_code_string X-Git-Tag: v3.1.8~13 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=554a368a506365e05be0a9c1f7d09bf7e5ebd745;p=thirdparty%2Fccache.git Correctly handle backslash in strings in hash_source_code_string --- diff --git a/hashutil.c b/hashutil.c index d69e629fd..6cc9e6cb7 100644 --- a/hashutil.c +++ b/hashutil.c @@ -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++; } diff --git a/test/test_hashutil.c b/test/test_hashutil.c index 225841a5a..4a72d8491 100644 --- a/test/test_hashutil.c +++ b/test/test_hashutil.c @@ -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