-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Json Arithmetic operation resulted in an overflow #609
Comments
This particular exception (at least within the JSON serializer code) comes from the internal runtime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/PooledByteBufferWriter.cs Line 141 in 57e5755
Both of those seem reasonable to me. Adding an if-check on the size that throws makes sense (though I don't know if, in some contexts, checking/throwing might introduce noticeable overhead). That said, it is possible that we have other edge cases where Anyone interested in a PR with tests? |
@ahsonkhan I'm interested, could you assign me? |
@felipepessoto done. |
@ahsonkhan currently the checked keyword only validates the integer overflow (if the value would exceed int.MaxValue). In this case, should we validate if the newSize is smaller than (int.MaxValue - 56) (for 64 bit process)? |
It's a bit more complicated than headers overhead. https://docs.microsoft.com/en-us/dotnet/api/system.array?view=netcore-3.1
|
Nice, I didn't know that. So we can compare it to Array.MaxByteArrayLength or 0X7FFFFFC7 |
Another question, throwing JsonException with a custom message is ok for this scenario? Or should I create a specific Exception? Thanks |
@felipepessoto The question about Json exceptions is tracked in #610 |
Using Actually, just clamping the result to
which seems like a reasonable error to me. |
@mikedn, if we let the array constructor throw the OutOfMemoryException, we'll be back to the original problem where the Exception message is not clear. I could validate the size and throw an OutOfMemoryException with a custom message. Stephen also gave a suggestion here: #1308 (comment) |
I don't think so, the original message is "Arithmetic operation resulted in an overflow." which is indeed not very clear, it's not obvious to anyone what the cause of the problem is. The "Array dimensions exceeded supported range" message should be obvious enough. Unless you really want to tell the user - "Hey, your JSON is yugeeee!" - so there's definitely no confusion :) Besides, this looks very much like a corner case. The fact that a 2GB JSON is first generated in memory and then written to a file seems pretty dubious to me. |
You are right. Agreed |
Re-opening until #32587 is done. |
We throw a OutOfMemoryException instead of "Arithmetic operation resulted in an overflow"
Re-opening until #34040 is done. |
Experimenting with new Json API in PowerShell Core repo I get very large output and as result exception "Arithmetic operation resulted in an overflow".
The exception message looks non-user-friendly and non-useful.
Proposal:
Additional information
The exception comes from
runtime/src/libraries/Common/src/System/Buffers/ArrayBufferWriter.cs
Line 177 in 00813df
Stack trace from PowerShell:
PowerShell can be downloaded from the PR PowerShell/PowerShell#11198
Repo scripts:
The text was updated successfully, but these errors were encountered: