Skip to content

Commit

Permalink
Fix comment: revert checked cast long length.
Browse files Browse the repository at this point in the history
  • Loading branch information
lateapexearlyspeed authored and eiriktsarpalis committed Jan 25, 2023
1 parent 96f1583 commit 2d3d4a5
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public static void ThrowArgumentException_PropertyNameTooLarge(int tokenLength)
}

[DoesNotReturn]
public static void ThrowArgumentException_ValueTooLarge(int tokenLength)
public static void ThrowArgumentException_ValueTooLarge(long tokenLength)
{
throw GetArgumentException(SR.Format(SR.ValueTooLarge, tokenLength));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void WriteRawValue([StringSyntax(StringSyntaxAttribute.Json)] ReadOnlySpa
/// </summary>
/// <param name="utf8Json">The raw JSON content to write.</param>
/// <param name="skipInputValidation">Whether to validate if the input is an RFC 8259-compliant JSON payload.</param>
/// <exception cref="ArgumentException">Thrown if the length of the input is zero or equal to <see cref="int.MaxValue"/>.</exception>
/// <exception cref="ArgumentException">Thrown if the length of the input is zero or greater than or equal to <see cref="int.MaxValue"/>.</exception>
/// <exception cref="JsonException">
/// Thrown if <paramref name="skipInputValidation"/> is <see langword="false"/>, and the input
/// is not a valid, complete, single JSON value according to the JSON RFC (https://tools.ietf.org/html/rfc8259)
Expand Down Expand Up @@ -141,15 +141,15 @@ public void WriteRawValue(ReadOnlySequence<byte> utf8Json, bool skipInputValidat
ValidateWritingValue();
}

int len = checked((int)utf8Json.Length);
long utf8JsonLen = utf8Json.Length;

if (len == 0)
if (utf8JsonLen == 0)
{
ThrowHelper.ThrowArgumentException(SR.ExpectedJsonTokens);
}
if (len == int.MaxValue)
if (utf8JsonLen >= int.MaxValue)
{
ThrowHelper.ThrowArgumentException_ValueTooLarge(int.MaxValue);
ThrowHelper.ThrowArgumentException_ValueTooLarge(utf8JsonLen);
}

if (skipInputValidation)
Expand All @@ -168,7 +168,8 @@ public void WriteRawValue(ReadOnlySequence<byte> utf8Json, bool skipInputValidat
_tokenType = reader.TokenType;
}

Debug.Assert(len < int.MaxValue);
Debug.Assert(utf8JsonLen < int.MaxValue);
int len = (int)utf8JsonLen;

// TODO (https://github.com/dotnet/runtime/issues/29293):
// investigate writing this in chunks, rather than requesting one potentially long, contiguous buffer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,15 +536,7 @@ public void WriteRawUtf8LengthGreaterThanOrEqualToIntMax(long len)
using MemoryStream ms = new();
using Utf8JsonWriter writer = new(ms);
ReadOnlySequence<byte> readonlySeq = CreateLargeReadOnlySequence(len);

if (len == int.MaxValue)
{
Assert.Throws<ArgumentException>(() => writer.WriteRawValue(readonlySeq));
}
else
{
Assert.Throws<OverflowException>(() => writer.WriteRawValue(readonlySeq));
}
Assert.Throws<ArgumentException>(() => writer.WriteRawValue(readonlySeq));
}
catch (OutOfMemoryException) { } // Perhaps failed to allocate large arrays
}
Expand Down

0 comments on commit 2d3d4a5

Please sign in to comment.