From 5a58184e5f7824c9a926f87e5d8e983464d44101 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Csaba=20Osztrogon=C3=A1c?= Date: Fri, 14 Aug 2020 22:22:32 +0000 Subject: [PATCH] Fix heap-buffer-overflow in ecma_builtin_json_quote MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes #4129. JerryScript-DCO-1.0-Signed-off-by: Csaba Osztrogonác csaba.osztrogonac@h-lab.eu --- .../ecma/builtin-objects/ecma-builtin-json.c | 12 ++++++++---- tests/jerry/es.next/regression-test-issue-4129.js | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 tests/jerry/es.next/regression-test-issue-4129.js diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-json.c b/jerry-core/ecma/builtin-objects/ecma-builtin-json.c index 21eb0da055..ed32aace63 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-json.c +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-json.c @@ -898,11 +898,15 @@ ecma_builtin_json_quote (ecma_stringbuilder_t *builder_p, /**< builder for the r #if ENABLED (JERRY_ESNEXT) if (lit_is_code_point_utf16_high_surrogate (c)) { - const ecma_char_t next_ch = lit_cesu8_peek_next (str_p); - if (lit_is_code_point_utf16_low_surrogate (next_ch)) + if (str_p < str_end_p) { - str_p += LIT_UTF8_MAX_BYTES_IN_CODE_UNIT; - continue; + const ecma_char_t next_ch = lit_cesu8_peek_next (str_p); + if (lit_is_code_point_utf16_low_surrogate (next_ch)) + { + str_p += LIT_UTF8_MAX_BYTES_IN_CODE_UNIT; + continue; + } + should_escape = true; } else { diff --git a/tests/jerry/es.next/regression-test-issue-4129.js b/tests/jerry/es.next/regression-test-issue-4129.js new file mode 100644 index 0000000000..cb299ea2ed --- /dev/null +++ b/tests/jerry/es.next/regression-test-issue-4129.js @@ -0,0 +1,15 @@ +// Copyright JS Foundation and other contributors, http://js.foundation +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +assert(JSON.stringify("\uD834") === '"\\ud834"');