Skip to content

Commit

Permalink
fix: crash when input ends with a truncated unicode character
Browse files Browse the repository at this point in the history
  • Loading branch information
josdejong committed Nov 13, 2023
1 parent 36de735 commit fe6a1e3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
10 changes: 9 additions & 1 deletion src/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
]

Expand Down
6 changes: 1 addition & 5 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()}`)
}

Expand Down

0 comments on commit fe6a1e3

Please sign in to comment.