Skip to content

Commit 6dab5ea

Browse files
author
Martijn The
committed
Fix buffer overrun while parsing malformed JSON hex escape sequence
Fixes #2200 JerryScript-DCO-1.0-Signed-off-by: Martijn The [email protected]
1 parent e860870 commit 6dab5ea

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@
4040
#define BUILTIN_UNDERSCORED_ID json
4141
#include "ecma-builtin-internal-routines-template.inc.h"
4242

43+
/**
44+
* The number of expected hexidecimal characters in a hex escape sequence (i.e. \ud801)
45+
*/
46+
#define ECMA_JSON_HEX_ESCAPE_SEQUENCE_LENGTH (4)
47+
4348
/** \addtogroup ecma ECMA
4449
* @{
4550
*
@@ -182,13 +187,17 @@ ecma_builtin_json_parse_string (ecma_json_token_t *token_p) /**< token argument
182187
}
183188
case LIT_CHAR_LOWERCASE_U:
184189
{
190+
if ((end_p - current_p < ECMA_JSON_HEX_ESCAPE_SEQUENCE_LENGTH + 1)) {
191+
return;
192+
}
193+
185194
ecma_char_t code_unit;
186195
if ((end_p - current_p >= 2) && !(lit_read_code_unit_from_hex (current_p + 1, 4, &code_unit)))
187196
{
188197
return;
189198
}
190199

191-
current_p += 5;
200+
current_p += ECMA_JSON_HEX_ESCAPE_SEQUENCE_LENGTH + 1;
192201

193202
lit_utf8_byte_t char_buffer[LIT_UTF8_MAX_BYTES_IN_CODE_UNIT];
194203
buffer_size += lit_code_unit_to_utf8 (code_unit, char_buffer);
@@ -258,9 +267,9 @@ ecma_builtin_json_parse_string (ecma_json_token_t *token_p) /**< token argument
258267
{
259268
ecma_char_t code_unit;
260269

261-
lit_read_code_unit_from_hex (current_p + 1, 4, &code_unit);
270+
lit_read_code_unit_from_hex (current_p + 1, ECMA_JSON_HEX_ESCAPE_SEQUENCE_LENGTH, &code_unit);
262271

263-
current_p += 5;
272+
current_p += ECMA_JSON_HEX_ESCAPE_SEQUENCE_LENGTH + 1;
264273
write_p += lit_code_unit_to_utf8 (code_unit, write_p);
265274
continue;
266275
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
[
16+
// This input caused a buffer overrun,
17+
// see https://github.com/jerryscript-project/jerryscript/issues/2200
18+
'"\\ubad',
19+
// Test similar malformations as well:
20+
'"\\ubad"',
21+
'"\\u',
22+
].forEach(function(badJson) {
23+
try {
24+
JSON.parse(badJson);
25+
} catch (e) {
26+
}
27+
});

0 commit comments

Comments
 (0)