Skip to content

Commit

Permalink
{fix} fail when encountering illegal unicode escapes
Browse files Browse the repository at this point in the history
Fixes #8
  • Loading branch information
bgotink committed Dec 23, 2024
1 parent 61b5b55 commit 85607a6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/string-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,19 @@ function replaceEscapes(ctx, value, token) {
hadError = true;
return "";
} else if (unicode) {
return String.fromCodePoint(parseInt(unicode, 16));
const codePoint = parseInt(unicode, 16);

// Non-scalar values
if (codePoint >= 0xd800 && codePoint <= 0xdfff) {
ctx.errors.push(
new InvalidKdlError(
String.raw`Invalid unicode escape "\u{${unicode}}, only scalar values can be added using an escape`,
{token},
),
);
}

return String.fromCodePoint(codePoint);
} else {
const replacement = escapedValues.get(escape);

Expand Down
23 changes: 23 additions & 0 deletions test/issues.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,27 @@ test("issue #5: trailing comments", () => {
assert.doesNotThrow(() => parse(`node "arg"\n\n// test\n`));
});

test("issue #8: escaped surrogates", () => {
assert.throws(
() => {
parse(
String.raw`no "Surrogtates high\u{D800}\u{D911}\u{DABB}\u{DBFF} low\u{DC00}\u{DEAD}\u{DFFF}"`,
);
},
(err) => {
assert(err instanceof AggregateError);
assert.equal(err.errors.length, 7);

for (let i = 0; i < 7; i++) {
assert.match(
err.errors[i].message,
/only scalar values can be added using an escape/,
);
}

return true;
},
);
});

test.run();

0 comments on commit 85607a6

Please sign in to comment.