Skip to content

Commit b828d1c

Browse files
authored
Fix heap-buffer-overflow in ecma_builtin_json_quote (#4143)
Fixes #4129. JerryScript-DCO-1.0-Signed-off-by: Csaba Osztrogonác [email protected]
1 parent d9cb2c6 commit b828d1c

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

jerry-core/ecma/builtin-objects/ecma-builtin-json.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -898,11 +898,15 @@ ecma_builtin_json_quote (ecma_stringbuilder_t *builder_p, /**< builder for the r
898898
#if ENABLED (JERRY_ESNEXT)
899899
if (lit_is_code_point_utf16_high_surrogate (c))
900900
{
901-
const ecma_char_t next_ch = lit_cesu8_peek_next (str_p);
902-
if (lit_is_code_point_utf16_low_surrogate (next_ch))
901+
if (str_p < str_end_p)
903902
{
904-
str_p += LIT_UTF8_MAX_BYTES_IN_CODE_UNIT;
905-
continue;
903+
const ecma_char_t next_ch = lit_cesu8_peek_next (str_p);
904+
if (lit_is_code_point_utf16_low_surrogate (next_ch))
905+
{
906+
str_p += LIT_UTF8_MAX_BYTES_IN_CODE_UNIT;
907+
continue;
908+
}
909+
should_escape = true;
906910
}
907911
else
908912
{
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright JS Foundation and other contributors, http://js.foundation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
assert(JSON.stringify("\uD834") === '"\\ud834"');

0 commit comments

Comments
 (0)