Skip to content

Commit

Permalink
Change exception message when reading numbers with leading zeros (dot…
Browse files Browse the repository at this point in the history
…net/corefx#40520)

* Change exception message when reading numbers with leading zeroes such as octals.

* Modified exception message and added condition to only throw it when an actual number is being read.

* Addressing suggestions.

* Narrowing scope to ConsumeZero and ConsumeZeroMultiSegment methods only.

* Addressing suggestion to use JsonHelpers.IsInRangeInclusive


Commit migrated from dotnet/corefx@7055b49
  • Loading branch information
jozkee authored Aug 27, 2019
1 parent d0872da commit ddcb3cb
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/libraries/System.Text.Json/src/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,9 @@
<data name="InvalidDuplicatePropertyNameHandling" xml:space="preserve">
<value>The DuplicatePropertyNameHandling enum must be set to one of the supported values.</value>
</data>
<data name="InvalidLeadingZeroInNumber" xml:space="preserve">
<value>Invalid leading zero before '{0}'.</value>
</data>
<data name="ArrayModifiedDuringIteration" xml:space="preserve">
<value>The JSON array was modified during iteration.</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1344,7 +1344,9 @@ private ConsumeNumberResult ConsumeZeroMultiSegment(ref ReadOnlySpan<byte> data,
if (nextByte != '.' && nextByte != 'E' && nextByte != 'e')
{
RollBackState(rollBackState, isError: true);
ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ExpectedEndOfDigitNotFound, nextByte);
ThrowHelper.ThrowJsonReaderException(ref this,
JsonHelpers.IsInRangeInclusive(nextByte, '0', '9') ? ExceptionResource.InvalidLeadingZeroInNumber : ExceptionResource.ExpectedEndOfDigitNotFound,
nextByte);
}

return ConsumeNumberResult.OperationIncomplete;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1584,7 +1584,9 @@ private ConsumeNumberResult ConsumeZero(ref ReadOnlySpan<byte> data, ref int i)
if (nextByte != '.' && nextByte != 'E' && nextByte != 'e')
{
_bytePositionInLine += i;
ThrowHelper.ThrowJsonReaderException(ref this, ExceptionResource.ExpectedEndOfDigitNotFound, nextByte);
ThrowHelper.ThrowJsonReaderException(ref this,
JsonHelpers.IsInRangeInclusive(nextByte, '0', '9') ? ExceptionResource.InvalidLeadingZeroInNumber : ExceptionResource.ExpectedEndOfDigitNotFound,
nextByte);
}

return ConsumeNumberResult.OperationIncomplete;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,9 @@ private static string GetResourceString(ref Utf8JsonReader json, ExceptionResour
case ExceptionResource.UnexpectedEndOfLineSeparator:
message = SR.Format(SR.UnexpectedEndOfLineSeparator);
break;
case ExceptionResource.InvalidLeadingZeroInNumber:
message = SR.Format(SR.InvalidLeadingZeroInNumber, character);
break;
default:
Debug.Fail($"The ExceptionResource enum value: {resource} is not part of the switch. Add the appropriate case and exception message.");
break;
Expand Down Expand Up @@ -649,6 +652,7 @@ internal enum ExceptionResource
UnexpectedEndOfLineSeparator,
ExpectedOneCompleteToken,
NotEnoughData,
InvalidLeadingZeroInNumber,
}

internal enum NumericType
Expand Down
3 changes: 3 additions & 0 deletions src/libraries/System.Text.Json/tests/Utf8JsonReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2039,6 +2039,9 @@ public static void InvalidJsonSplitRemainsInvalid(string jsonString, int splitLo
[InlineData("123,", 0, 3, 64)]
[InlineData("01", 0, 1, 64)]
[InlineData("-01", 0, 2, 64)]
[InlineData("001", 0, 1, 64)]
[InlineData("00h", 0, 1, 64)]
[InlineData("[01", 0, 2, 64)]
[InlineData("10.5e-0.2", 0, 7, 64)]
[InlineData("{\"a\u6F22\u5B57ge\":30, \"ints\":[1, 2, 3, 4, 5.1e7.3]}", 0, 42, 64)]
[InlineData("{\"a\u6F22\u5B57ge\":30, \r\n \"num\":-0.e, \r\n \"ints\":[1, 2, 3, 4, 5]}", 1, 10, 64)]
Expand Down

0 comments on commit ddcb3cb

Please sign in to comment.