From fe6a1e3d1cb92665b8a152fb0cf1686fb9e7ca25 Mon Sep 17 00:00:00 2001 From: Jos de Jong Date: Mon, 13 Nov 2023 17:49:47 +0100 Subject: [PATCH] fix: crash when input ends with a truncated unicode character --- src/parse.test.ts | 10 +++++++++- src/parse.ts | 6 +----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/parse.test.ts b/src/parse.test.ts index ab64762..2c80eb7 100644 --- a/src/parse.test.ts +++ b/src/parse.test.ts @@ -388,7 +388,15 @@ describe('throw meaningful exceptions', () => { }, { input: 'foo', expectedError: "JSON value expected but got 'f' at position 0" }, { input: '"\\a"', expectedError: "Invalid escape character '\\a' at position 1" }, - { input: '"\\u26"', expectedError: "Invalid unicode character '\\u26' at position 1" }, + { input: '"\\u2', expectedError: "Invalid unicode character '\\u2' at position 1" }, + { input: '"\\u26', expectedError: "Invalid unicode character '\\u26' at position 1" }, + { input: '"\\u260', expectedError: "Invalid unicode character '\\u260' at position 1" }, + { + input: '"\\u2605', + expectedError: "End of string '\"' expected but reached end of input at position 7" + }, + { input: '{"s \\ud', expectedError: "Invalid unicode character '\\ud' at position 4" }, + { input: '"\\u26"', expectedError: "Invalid unicode character '\\u26\"' at position 1" }, { input: '"\\uZ000"', expectedError: "Invalid unicode character '\\uZ000' at position 1" } ] diff --git a/src/parse.ts b/src/parse.ts index e91f35a..29cc174 100644 --- a/src/parse.ts +++ b/src/parse.ts @@ -300,11 +300,7 @@ export function parse( } function throwInvalidUnicodeCharacter(start: number) { - let end = start + 2 - while (/\w/.test(text[end])) { - end++ - } - const chars = text.slice(start, end) + const chars = text.slice(start, start + 6) throw new SyntaxError(`Invalid unicode character '${chars}' ${pos()}`) }