Fix hash calculation for strings created using jerry_create_string_from_utf8 (#1912)

The hash was being calculated over the bytes *after* the actual string bytes.

JerryScript-DCO-1.0-Signed-off-by: Martijn The martijn.the@intel.com
This commit is contained in:
Martijn Thé
2017-07-10 08:53:58 +02:00
committed by Zoltan Herczeg
parent 18283d22c0
commit 91912689ad
2 changed files with 29 additions and 1 deletions
+2 -1
View File
@@ -298,6 +298,7 @@ ecma_new_ecma_string_from_utf8_converted_to_cesu8 (const lit_utf8_byte_t *string
data_p = (lit_utf8_byte_t *) (long_string_desc_p + 1);
}
const lit_utf8_byte_t *const begin_data_p = data_p;
pos = 0;
while (pos < string_size)
@@ -324,7 +325,7 @@ ecma_new_ecma_string_from_utf8_converted_to_cesu8 (const lit_utf8_byte_t *string
JERRY_ASSERT (pos == string_size);
string_desc_p->hash = lit_utf8_string_calc_hash (data_p, converted_string_size);
string_desc_p->hash = lit_utf8_string_calc_hash (begin_data_p, converted_string_size);
}
return string_desc_p;
+27
View File
@@ -338,6 +338,30 @@ test_run_simple (const char *script_p) /**< source code to run */
return jerry_run_simple ((const jerry_char_t *) script_p, script_size, JERRY_INIT_EMPTY);
} /* test_run_simple */
static bool
strict_equals (jerry_value_t a,
jerry_value_t b)
{
bool is_strict_equal;
const char *is_equal_src;
jerry_value_t args[2];
jerry_value_t is_equal_fn_val;
jerry_value_t res;
is_equal_src = "var isEqual = function(a, b) { return (a === b); }; isEqual";
is_equal_fn_val = jerry_eval ((jerry_char_t *) is_equal_src, strlen (is_equal_src), false);
TEST_ASSERT (!jerry_value_has_error_flag (is_equal_fn_val));
args[0] = a;
args[1] = b;
res = jerry_call_function (is_equal_fn_val, jerry_create_undefined (), args, 2);
TEST_ASSERT (!jerry_value_has_error_flag (res));
TEST_ASSERT (jerry_value_is_boolean (res));
is_strict_equal = jerry_get_boolean_value (res);
jerry_release_value (res);
jerry_release_value (is_equal_fn_val);
return is_strict_equal;
} /* strict_equals */
int
main (void)
{
@@ -402,6 +426,9 @@ main (void)
args[0] = jerry_create_string_from_utf8 ((jerry_char_t *) "\x73\x74\x72\x3a \xf0\x90\x90\x80");
args[1] = jerry_create_string ((jerry_char_t *) "\x73\x74\x72\x3a \xed\xa0\x81\xed\xb0\x80");
/* Test that the strings are equal / ensure hashes are equal */
TEST_ASSERT (strict_equals (args[0], args[1]));
/* These sizes must be equal */
utf8_sz = jerry_get_utf8_string_size (args[0]);
cesu8_sz = jerry_get_utf8_string_size (args[1]);