From 513130cab178754346747dd5a16b4eba7ad03015 Mon Sep 17 00:00:00 2001 From: Peter Gal Date: Thu, 29 Mar 2018 16:29:12 +0200 Subject: [PATCH] Fix accessing the contents of a direct string In the `ecma_string_get_chars` method the contents of a direct string is accessed incorrectly. It tries to extract the magic string id from the string pointer but the direct string does not need this step. JerryScript-DCO-1.0-Signed-off-by: Peter Gal pgal.u-szeged@partner.samsung.com --- jerry-core/ecma/base/ecma-helpers-string.c | 5 ++--- tests/unit-core/test-api.c | 9 +++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/jerry-core/ecma/base/ecma-helpers-string.c b/jerry-core/ecma/base/ecma-helpers-string.c index 44e186a621..0ba25a2d00 100644 --- a/jerry-core/ecma/base/ecma-helpers-string.c +++ b/jerry-core/ecma/base/ecma-helpers-string.c @@ -1458,14 +1458,13 @@ ecma_string_get_chars (const ecma_string_t *string_p, /**< ecma-string */ lit_magic_string_ex_id_t id = (lit_magic_string_ex_id_t) ECMA_GET_DIRECT_STRING_VALUE (string_p); size = lit_get_magic_string_ex_size (id); + result_p = lit_get_magic_string_ex_utf8 (id); length = 0; if (unlikely (*flags_p & ECMA_STRING_FLAG_IS_ASCII)) { - length = lit_utf8_string_length (lit_get_magic_string_ex_utf8 (string_p->u.magic_string_ex_id), size); + length = lit_utf8_string_length (result_p, size); } - - result_p = lit_get_magic_string_ex_utf8 (id); break; } } diff --git a/tests/unit-core/test-api.c b/tests/unit-core/test-api.c index 948d198762..643d26a6d9 100644 --- a/tests/unit-core/test-api.c +++ b/tests/unit-core/test-api.c @@ -1118,6 +1118,15 @@ main (void) jerry_release_value (args[0]); + const char *test_magic_str_access_src_p = "'console'.charAt(6) == 'e'"; + res = jerry_eval ((const jerry_char_t *) test_magic_str_access_src_p, + strlen (test_magic_str_access_src_p), + false); + TEST_ASSERT (jerry_value_is_boolean (res)); + TEST_ASSERT (jerry_get_boolean_value (res) == true); + + jerry_release_value (res); + cesu8_length = jerry_get_string_length (args[1]); cesu8_sz = jerry_get_string_size (args[1]);