From 955670174262590c0ecc83f2ba74f473283da345 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A9ter=20G=C3=A1l?= Date: Fri, 26 Feb 2021 10:53:54 +0100 Subject: [PATCH] Add size checks before using JERRY_VLA in unittests (#4601) JerryScript-DCO-1.0-Signed-off-by: Peter Gal pgal.usz@partner.samsung.com --- tests/unit-core/test-arraybuffer.c | 3 ++- tests/unit-core/test-json.c | 4 +++- tests/unit-core/test-regexp.c | 6 ++++-- tests/unit-core/test-typedarray.c | 3 ++- 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/tests/unit-core/test-arraybuffer.c b/tests/unit-core/test-arraybuffer.c index dd3ca5186..85f83f758 100644 --- a/tests/unit-core/test-arraybuffer.c +++ b/tests/unit-core/test-arraybuffer.c @@ -55,7 +55,8 @@ assert_handler (const jerry_call_info_t *call_info_p, /**< call information */ && jerry_value_is_string (args_p[1])) { jerry_length_t utf8_sz = jerry_get_string_size (args_p[1]); - JERRY_VLA (char, string_from_utf8, utf8_sz); + TEST_ASSERT (utf8_sz <= 127); /* 127 is the expected max assert fail message size. */ + JERRY_VLA (char, string_from_utf8, utf8_sz + 1); string_from_utf8[utf8_sz] = 0; jerry_string_to_char_buffer (args_p[1], (jerry_char_t *) string_from_utf8, utf8_sz); diff --git a/tests/unit-core/test-json.c b/tests/unit-core/test-json.c index 7390c0ec4..72710ab14 100644 --- a/tests/unit-core/test-json.c +++ b/tests/unit-core/test-json.c @@ -53,6 +53,7 @@ main (void) TEST_ASSERT (jerry_value_is_string (name_value) == true); jerry_size_t name_size = jerry_get_string_size (name_value); + TEST_ASSERT (name_size == 4); JERRY_VLA (jerry_char_t, name_data, name_size + 1); jerry_size_t copied = jerry_string_to_char_buffer (name_value, name_data, name_size); name_data[name_size] = '\0'; @@ -127,12 +128,13 @@ main (void) jerry_release_value (obj); + const char check_value[] = "{\"name\":\"John\",\"age\":32}"; jerry_size_t json_size = jerry_get_string_size (json_string); + TEST_ASSERT (json_size == strlen (check_value)); JERRY_VLA (jerry_char_t, json_data, json_size + 1); jerry_string_to_char_buffer (json_string, json_data, json_size); json_data[json_size] = '\0'; - const char check_value[] = "{\"name\":\"John\",\"age\":32}"; TEST_ASSERT_STR (check_value, json_data); jerry_release_value (json_string); diff --git a/tests/unit-core/test-regexp.c b/tests/unit-core/test-regexp.c index 2c7ac0429..c7c069595 100644 --- a/tests/unit-core/test-regexp.c +++ b/tests/unit-core/test-regexp.c @@ -47,12 +47,14 @@ main (void) jerry_value_t is_multiline = jerry_get_property_by_index (res, 1); jerry_value_t is_global = jerry_get_property_by_index (res, 2); + const char expected_result[] = "something"; jerry_size_t str_size = jerry_get_string_size (regex_res_str); + TEST_ASSERT (str_size == (sizeof (expected_result) - 1)); + JERRY_VLA (jerry_char_t, res_buff, str_size); jerry_size_t res_size = jerry_string_to_char_buffer (regex_res_str, res_buff, str_size); - const char expected_result[] = "something"; - TEST_ASSERT (res_size == (sizeof (expected_result) - 1)); + TEST_ASSERT (res_size == str_size); TEST_ASSERT (strncmp (expected_result, (const char *) res_buff, res_size) == 0); TEST_ASSERT (jerry_get_boolean_value (is_multiline)); TEST_ASSERT (jerry_get_boolean_value (is_global)); diff --git a/tests/unit-core/test-typedarray.c b/tests/unit-core/test-typedarray.c index 65a03cc25..f500d58ec 100644 --- a/tests/unit-core/test-typedarray.c +++ b/tests/unit-core/test-typedarray.c @@ -67,7 +67,8 @@ assert_handler (const jerry_call_info_t *call_info_p, /**< call information */ && jerry_value_is_string (args_p[1])) { jerry_length_t utf8_sz = jerry_get_string_size (args_p[1]); - JERRY_VLA (char, string_from_utf8, utf8_sz); + TEST_ASSERT (utf8_sz <= 127); /* 127 is the expected max assert fail message size. */ + JERRY_VLA (char, string_from_utf8, utf8_sz + 1); string_from_utf8[utf8_sz] = 0; jerry_string_to_char_buffer (args_p[1], (jerry_char_t *) string_from_utf8, utf8_sz);