From 6d14feffe699ad14c340a23043bd9d7904c06432 Mon Sep 17 00:00:00 2001 From: Alexandre Mutel Date: Sun, 3 Feb 2019 19:05:51 +0100 Subject: [PATCH] Fix invalid unicode scalar value --- src/SharpToml/Parsing/Lexer.cs | 5 +++++ src/SharpToml/Text/CharHelper.cs | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/src/SharpToml/Parsing/Lexer.cs b/src/SharpToml/Parsing/Lexer.cs index b1bc4bd..57c40e4 100644 --- a/src/SharpToml/Parsing/Lexer.cs +++ b/src/SharpToml/Parsing/Lexer.cs @@ -821,6 +821,7 @@ private bool TryReadEscapeChar(ref TextPosition end) case 'u': case 'U': { + var start = _position; end = _position; var maxCount = _c == 'u' ? 4 : 8; NextChar(); @@ -837,6 +838,10 @@ private bool TryReadEscapeChar(ref TextPosition end) if (i == maxCount) { + if (!CharHelper.IsValidUnicodeScalarValue(value)) + { + AddError($"Invalid Unicode scalar value [{value:X}]",start, start); + } _textBuilder.AppendUtf32((char32)value); return true; } diff --git a/src/SharpToml/Text/CharHelper.cs b/src/SharpToml/Text/CharHelper.cs index 4f0dcf2..42b0153 100644 --- a/src/SharpToml/Text/CharHelper.cs +++ b/src/SharpToml/Text/CharHelper.cs @@ -42,6 +42,11 @@ public static bool IsIdentifierContinue(char32 c) return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); } + public static bool IsValidUnicodeScalarValue(char32 c) + { + return c >= 0 && c <= 0xD7FF || c >= 0xE000 && c < 0x10FFFF; + } + public static bool IsDigit(char32 c) { return (c >= '0' && c <= '9');