Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix control character bug #51

Merged
merged 3 commits into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/Tomlyn.Tests/ValidatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,20 @@ public void TestTableArrayAndInvalidTable()
Assert.True(doc.HasErrors, "The document should have errors");
}

[Test]
public void TestValidControlCharacters()
{
var input = "";
input += "backspace='\b'\n";
input += "tab='\t'\n";
input += "form_feed='\f'\n";
input += "quote='\"'\n";
input += "backslash='\\'\n";
input += "heart='\u2665'\n";
input += "some_unicode='\U0002B695'\n";
var doc = Toml.Parse(input);
Assert.False(doc.HasErrors, "The document should not have errors on valid control characters");
}

}
}
2 changes: 1 addition & 1 deletion src/Tomlyn/Parsing/Lexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,7 @@ private void ReadStringLiteral(TextPosition start, bool allowMultiline)
{
AddError("Invalid newline in a string", _position, _position);
}
else if (CharHelper.IsControlCharacter(_c) && (!isMultiLine || !CharHelper.IsNewLine(_c)))
else if (CharHelper.IsControlCharacter(_c) && !CharHelper.IsNonNewlineValidControlCharacter(_c) && (!isMultiLine || !CharHelper.IsNewLine(_c)))
{
AddError($"Invalid control character found {((char)_c).ToPrintableString()}", start, start);
}
Expand Down
5 changes: 5 additions & 0 deletions src/Tomlyn/Text/CharHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public static bool IsControlCharacter(char32 c)
return c <= 0x1F || c == 0x7F;
}

public static bool IsNonNewlineValidControlCharacter(char32 c)
{
return c == '\b' || c == '\t' || c == '\f';
xoofx marked this conversation as resolved.
Show resolved Hide resolved
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsKeyStart(char32 c)
{
Expand Down