You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
IndexOutOfRangeException in the NumericHandler #4313
We recently ran into an issue related to the conversion of a value of the numeric Postgres type to the decimal .NET type. This exception was thrown in an application that uses EF Core, but after examining the stack trace and reproducing the exception using the npgsql test suite, it seemed better to raise the issue in this repository.
Steps to reproduce
I've added a test case to your unit test suite which captures the bug. You can see the branch here. Here's the actual commit. I imagine this will be more useful to you than the following, but you should also be able to reproduce this by following these steps:
Create a table with a numeric column and insert a row which has the value "95909.8281939472410410151370800".
Select that column and attempt to convert the result to a decimal.
Observe that IndexOutOfRangeException is thrown in the Npgsql.Internal.TypeHandlers.NumericHandlers.NumericHandler class.
The issue
It's expected that a decimal with the value 95909.82819394724104101513708M will be returned.
The exception that is encountered is given below:
Exception message: Index was outside the bounds of the array.
Stack trace:
at Npgsql.Internal.TypeHandlers.NumericHandlers.NumericHandler.<Read>d__11.MoveNext() in C:\Users\MattCarr\source\repos\npgsql\src\Npgsql\Internal\TypeHandlers\NumericHandlers\NumericHandler.cs:line 94
Further technical details
Npgsql version: Commit bc518ec
PostgreSQL version: v13.3
Operating system: Microsoft Windows [Version 10.0.19044.1466]
Other details about my project setup:
Postgres is running in a docker container.
Further investigation
The exception is thrown when the DecimalRaw.Powers10 array is indexed with -1:
I confess my ignorance of the implementation of the numeric type, but the fact that the local variable groups is 0 might be why things went off the rails:
That number is outside the valid range for a System.Decimal, due to those trailing zeros (we decided earlier that bunch of trailing zeros, leading to overflow, should fail). A System.Decimal may only have 29 visible digits. Your number is 30 digits.
I think the correct fix is therefore, just before the line were the crashing array access is, to throw a proper OverflowException whenever scaleDifference is positive. We should also add a check that the absolute value of the number when the decimal separator is removed, is less than 2^96, unless this is already handled in DecimalRaw.
It's unfortunate that insignificant zeros can cause an otherwise valid numeric value from being converted, but I understand that this is a design decision that has already been adopted. Throwing the overflow exception makes sense. It would be helpful if the exception message included a hint to the user that the error could be resolved by reducing the number of decimal places in the numeric value.
@KernelMC note that in .NET as well, 1.0000m preserves the four zeros (e.g. prints them back), so the value is different than e.g. 1.0m (even if they're both equal).
A better exception makes sense, though we should probably see what happens with #4299 first.
@roji, I didn't know that decimal stores all provided decimal places, even "insignificant zeros". I see now that it totally makes sense for this to be an error condition as you aren't able to faithfully represent the given numeric value as a decimal.
Activity
roji commentedon Feb 4, 2022
@Emill IIRC you worked on this code... Any chance you have some time to take a look? Note that there's also #4299 with two issues also on numeric.
Emill commentedon Feb 4, 2022
Ok I'll take a look.
roji commentedon Feb 4, 2022
Thank you!
Emill commentedon Feb 7, 2022
That number is outside the valid range for a System.Decimal, due to those trailing zeros (we decided earlier that bunch of trailing zeros, leading to overflow, should fail). A System.Decimal may only have 29 visible digits. Your number is 30 digits.
I think the correct fix is therefore, just before the line were the crashing array access is, to throw a proper OverflowException whenever scaleDifference is positive. We should also add a check that the absolute value of the number when the decimal separator is removed, is less than 2^96, unless this is already handled in DecimalRaw.
KernelMC commentedon Feb 8, 2022
It's unfortunate that insignificant zeros can cause an otherwise valid numeric value from being converted, but I understand that this is a design decision that has already been adopted. Throwing the overflow exception makes sense. It would be helpful if the exception message included a hint to the user that the error could be resolved by reducing the number of decimal places in the numeric value.
Thank you for looking into this!
roji commentedon Feb 8, 2022
@KernelMC note that in .NET as well,
1.0000mpreserves the four zeros (e.g. prints them back), so the value is different than e.g.1.0m(even if they're both equal).A better exception makes sense, though we should probably see what happens with #4299 first.
KernelMC commentedon Feb 8, 2022
@roji, I didn't know that
decimalstores all provided decimal places, even "insignificant zeros". I see now that it totally makes sense for this to be an error condition as you aren't able to faithfully represent the givennumericvalue as adecimal.NinoFloris commentedon Aug 15, 2023
Closing as it's a duplicate of #4299