Skip to content

IndexOutOfRangeException in the NumericHandler #4313

Closed as not planned
Closed as not planned

Description

@KernelMC

Summary

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:

  1. Create a table with a numeric column and insert a row which has the value "95909.8281939472410410151370800".
  2. Select that column and attempt to convert the result to a decimal.
  3. 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:
image

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:
image

Activity

  1. roji commented on Feb 4, 2022

    @roji
    Member

    @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.

  2. Emill commented on Feb 4, 2022

    @Emill
    Contributor

    Ok I'll take a look.

  3. roji commented on Feb 4, 2022

    @roji
    Member

    Thank you!

  4. Emill commented on Feb 7, 2022

    @Emill
    Contributor

    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.

  5. KernelMC commented on Feb 8, 2022

    @KernelMC
    Author

    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!

  6. roji commented on Feb 8, 2022

    @roji
    Member

    @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.

  7. KernelMC commented on Feb 8, 2022

    @KernelMC
    Author

    @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.

  8. added this to the Backlog milestone on Mar 9, 2023
  9. NinoFloris commented on Aug 15, 2023

    @NinoFloris
    Member

    Closing as it's a duplicate of #4299

  10. removed this from the Backlog milestone on Nov 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions