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
+12 -1
View File
@@ -118,6 +118,17 @@ namespace Newtonsoft.Json.Tests
}, "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]
public void PopulateObjectWithOnlyComment()
{
@@ -1830,4 +1841,4 @@ namespace Newtonsoft.Json.Tests
public DateTime Expiration { get; set; }
}
}
}
}
+5
View File
@@ -1937,6 +1937,11 @@ namespace Newtonsoft.Json
throw JsonReaderException.Create(this, "Unexpected character while parsing constructor: {0}.".FormatWith(CultureInfo.InvariantCulture, currentChar));
}
}
if (initialPosition == endPosition)
{
throw JsonReaderException.Create(this, "Empty constructor name.");
}
_stringReference = new StringReference(_chars, initialPosition, endPosition - initialPosition);
string constructorName = _stringReference.ToString();