Skip to content

Commit

Permalink
Fixes axuno#211
Browse files Browse the repository at this point in the history
Illegal characters that are not 8-bit, will not throw unexpected ThrowByteOverflowException
  • Loading branch information
axunonb committed Oct 20, 2021
1 parent caebf10 commit 0e83366
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
19 changes: 19 additions & 0 deletions src/SmartFormat.Tests/Core/ParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,25 @@ public void Parser_Throws_Exceptions(string format)
Assert.Throws<ParsingErrors>(() => formatter.Test(format, args, "Error"));
}

[TestCase("{V(LU)}")] // braces are illegal
[TestCase("{V LU }")] // blanks are illegal
[TestCase("{VĀLUĒ}")] // 0x100 and 0x112 are illegal chars
public void Parser_Throws_On_Illegal_Selector_Chars(string format)
{
var parser = GetRegularParser();
try
{
parser.ParseFormat(format);
Assert.That(true, "Should throw");
}
catch (Exception e)
{
// Throws, because selector contains 2 illegal characters
Assert.That(e, Is.InstanceOf<ParsingErrors>());
Assert.That(((ParsingErrors)e).Issues.Count, Is.EqualTo(2));
}
}

[Test]
public void Parser_Exception_ErrorDescription()
{
Expand Down
4 changes: 2 additions & 2 deletions src/SmartFormat/Core/Parsing/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ private void ParseSelector(ref Placeholder? currentPlaceholder, ParsingErrors pa
// Ensure the selector characters are valid:
if (!_validSelectorChars.Contains(inputChar))
parsingErrors.AddIssue(_resultFormat,
$"'0x{Convert.ToByte(inputChar):X}': " +
$"'0x{Convert.ToUInt32(inputChar):X}': " +
_parsingErrorText[ParsingError.InvalidCharactersInSelector],
_index.Current, _index.SafeAdd(_index.Current, 1));
}
Expand All @@ -636,7 +636,7 @@ private void AddLastSelector(ref Placeholder currentPlaceholder, ParsingErrors p
currentPlaceholder.AddSelector(new Selector(Settings, _inputFormat, _index.LastEnd, _index.Current,_index.Operator, _index.Selector));
else if (_index.Operator != _index.Current) // the selector only contains illegal ("trailing") operator characters
parsingErrors.AddIssue(_resultFormat,
$"'0x{Convert.ToByte(_inputFormat[_index.Operator]):X}': " +
$"'0x{Convert.ToInt32(_inputFormat[_index.Operator]):X}': " +
_parsingErrorText[ParsingError.TrailingOperatorsInSelector],
_index.Operator, _index.Current);
_index.LastEnd = _index.SafeAdd(_index.Current, 1);
Expand Down

0 comments on commit 0e83366

Please sign in to comment.