Fix handling of empty constructor names (#2922)

Co-authored-by: James Newton-King <james@newtonking.com>
This commit is contained in:
George Pollard
2023-12-07 08:31:00 +08:00
committed by GitHub
co-authored by James Newton-King
parent 01e1759cac
commit 55a7204f9b
2 changed files with 17 additions and 1 deletions
@@ -118,6 +118,17 @@ namespace Newtonsoft.Json.Tests
}, "No JSON content found. Path '', line 0, position 0."); }, "No JSON content found. Path '', line 0, position 0.");
} }
[Test]
public void NoConstructorName() {
ExceptionAssert.Throws<JsonException>(
() => JsonConvert.DeserializeObject("[new \0("),
"Empty constructor name. Path '', line 1, position 6.");
ExceptionAssert.Throws<JsonException>(
() => JsonConvert.DeserializeObject("{'x':new \0("),
"Empty constructor name. Path 'x', line 1, position 10.");
}
[Test] [Test]
public void PopulateObjectWithOnlyComment() public void PopulateObjectWithOnlyComment()
{ {
+5
View File
@@ -1938,6 +1938,11 @@ namespace Newtonsoft.Json
} }
} }
if (initialPosition == endPosition)
{
throw JsonReaderException.Create(this, "Empty constructor name.");
}
_stringReference = new StringReference(_chars, initialPosition, endPosition - initialPosition); _stringReference = new StringReference(_chars, initialPosition, endPosition - initialPosition);
string constructorName = _stringReference.ToString(); string constructorName = _stringReference.ToString();