Skip to content

Commit

Permalink
Allow tabs in property names when parsing JSON (#898)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma authored May 18, 2021
1 parent 0dbcdb0 commit ab88152
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
16 changes: 16 additions & 0 deletions Jint.Tests/Runtime/JsonTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Xunit;

namespace Jint.Tests.Runtime
{
public class JsonTests
{
[Fact]
public void CanParseTabsInProperties()
{
var engine = new Engine();
const string script = @"JSON.parse(""{\""abc\\tdef\"": \""42\""}"");";
var obj = engine.Execute(script).GetCompletionValue().AsObject();
Assert.True(obj.HasOwnProperty("abc\tdef"));
}
}
}
14 changes: 6 additions & 8 deletions Jint/Native/Json/JsonParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ private void ThrowError(Token token, string messageFormat, params object[] argum
index: token.Range[0],
position: new Position(token.LineNumber ?? _lineNumber, token.Range[0] - _lineStart + 1));
var exception = new ParserException("Line " + lineNumber + ": " + msg, error);

throw exception;
}

Expand Down Expand Up @@ -711,7 +711,7 @@ public ObjectInstance ParseJsonObject()
}

var name = Lex().Value.ToString();
if (PropertyNameContainsInvalidChar0To31(name))
if (PropertyNameContainsInvalidCharacters(name))
{
ExceptionHelper.ThrowSyntaxError(_engine, $"Invalid character in property name '{name}'");
}
Expand All @@ -732,14 +732,12 @@ public ObjectInstance ParseJsonObject()
return obj;
}

private static bool PropertyNameContainsInvalidChar0To31(string s)
private static bool PropertyNameContainsInvalidCharacters(string propertyName)
{
const int max = 31;

for (var i = 0; i < s.Length; i++)
const char max = (char) 31;
foreach (var c in propertyName)
{
var val = (int)s[i];
if (val <= max)
if (c != '\t' && c <= max)
return true;
}
return false;
Expand Down

0 comments on commit ab88152

Please sign in to comment.