Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -172,21 +172,38 @@ internal static void ValidateNumber(ReadOnlySpan<byte> utf8FormattedNumber)
return;
}

// The non digit character inside the number
byte val = utf8FormattedNumber[i];

if (val == '.')
{
i++;

while (i < utf8FormattedNumber.Length && JsonHelpers.IsDigit(utf8FormattedNumber[i]))
{
i++;
}

if (utf8FormattedNumber.Length < i)
{
throw new ArgumentException(SR.RequiredDigitNotFoundEndOfData, nameof(utf8FormattedNumber));
}
}
else if (val == 'e' || val == 'E')

if (i == utf8FormattedNumber.Length)
{
return;
}

val = utf8FormattedNumber[i];

if (val == 'e' || val == 'E')
{
i++;

if (i >= utf8FormattedNumber.Length)
if (utf8FormattedNumber.Length <= i)
Comment thread
bartonjs marked this conversation as resolved.
{
throw new ArgumentException(
SR.RequiredDigitNotFoundEndOfData,
nameof(utf8FormattedNumber));
throw new ArgumentException(SR.RequiredDigitNotFoundEndOfData, nameof(utf8FormattedNumber));
}

val = utf8FormattedNumber[i];
Expand All @@ -203,11 +220,9 @@ internal static void ValidateNumber(ReadOnlySpan<byte> utf8FormattedNumber)
nameof(utf8FormattedNumber));
}

if (i >= utf8FormattedNumber.Length)
if (utf8FormattedNumber.Length <= i)
{
throw new ArgumentException(
SR.RequiredDigitNotFoundEndOfData,
nameof(utf8FormattedNumber));
throw new ArgumentException(SR.RequiredDigitNotFoundEndOfData, nameof(utf8FormattedNumber));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do the tests hit every one of these throws?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently hitting the throws requires either a bug in this method (how we got here 😄) or a JsonElement having gotten into an invalid/corrupted state. (This method is here to guard against that corruption and to serve as validation for a future addition to the writer where it allows you to write the numeric string directly)

Having written both this method and JsonElement, it's probably most efficient for me to write the tests to get the coverage on these throws... after we get this PR in so we get the broken success scenario fixed.

}

while (i < utf8FormattedNumber.Length && JsonHelpers.IsDigit(utf8FormattedNumber[i]))
Expand Down
36 changes: 32 additions & 4 deletions src/System.Text.Json/tests/JsonElementWriteTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,39 @@ public static void WriteNumber(bool indented)
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public static void WriteNumberScientific(bool indented)
[InlineData("1e6", false)]
[InlineData("1e6", true)]
[InlineData("1e+6", false)]
[InlineData("1e+6", true)]
[InlineData("1e-6", false)]
[InlineData("1e-6", true)]
[InlineData("-1e6", false)]
[InlineData("-1e6", true)]
[InlineData("-1e+6", true)]
[InlineData("-1e+6", true)]
[InlineData("-1e-6", false)]
[InlineData("-1e-6", true)]
public static void WriteNumberScientific(string value, bool indented)
{
WriteSimpleValue(indented, value);
}

[Theory]
[InlineData("5.012e-20", false)]
[InlineData("5.012e-20", true)]
[InlineData("5.012e20", false)]
[InlineData("5.012e20", true)]
[InlineData("5.012e+20", false)]
[InlineData("5.012e+20", true)]
[InlineData("-5.012e-20", false)]
[InlineData("-5.012e-20", true)]
[InlineData("-5.012e20", false)]
[InlineData("-5.012e20", true)]
[InlineData("-5.012e+20", false)]
[InlineData("-5.012e+20", true)]
public static void WriteNumberDecimalScientific(string value, bool indented)
{
WriteSimpleValue(indented, "1e6");
WriteSimpleValue(indented, value);
}

[Theory]
Expand Down
1 change: 1 addition & 0 deletions src/System.Text.Json/tests/Utf8JsonWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3747,6 +3747,7 @@ public void WriteNumbers(bool formatted, bool skipValidation, string keyString)
doubles[2] = double.MinValue;
doubles[3] = 12.345e1;
doubles[4] = -123.45e1;

for (int i = 5; i < numberOfItems; i++)
{
var value = random.NextDouble();
Expand Down