Skip to content

Parsing Decimal With Negative Exponent And Zero Scale Can Overflow #5762

Closed

Description

@tustvold

Describe the bug

This passes as expected

let v = parse_decimal::<Decimal128Type>("99999999999999999999999999999999999999", 38, 0).unwrap();
assert_eq!(v, 99999999999999999999999999999999999999);

But if we add an additional 9 followed by e-1 we get overflow

let v = parse_decimal::<Decimal128Type>("999999999999999999999999999999999999999e-1", 38, 0).unwrap();
assert_eq!(v, 99999999999999999999999999999999999999);

To Reproduce

Expected behavior

Additional context

Activity

  1. added
    bug
    on May 13, 2024
  2. changed the title [-]Parsing Decimal With Negative Exponent Can Overflow[/-] [+]Parsing Decimal With Negative Exponent And Zero Scale Can Overflow[/+] on May 13, 2024
  3. alamb commented on May 13, 2024

    @alamb
    Contributor

    @yjshen would you be willing to look into this issue?

  4. yjshen commented on May 13, 2024

    @yjshen
    Member

    Yes, I'll look into this.

  5. self-assigned this
    on May 13, 2024
  6. ByteBaker commented on Oct 10, 2024

    @ByteBaker
    Contributor

    @yjshen any update on this?

  7. neilconway commented on Aug 21, 2026

    @neilconway
    Contributor

    take

  8. sdf-jkl commented on Sep 30, 2026

    @sdf-jkl
    Member

    Closed via #10850. The parser now handles oversized mantissas with negative exponents when the rounded result fits the target precision.

    The original repro still returns an error, correctly under the new rounding semantics: the value is 38 nines followed by .9, which rounds to 10^38 at scale 0 and exceeds precision 38. The expected result in this issue assumed truncation. #10850 adds regression coverage for both representable results and this rounding-overflow case:

    // More digits than an i128 can hold, but a small value
    ("10000000000000000000000000000000000000000e-39", 10i128, 0),
    // Digits beyond the scale round; here the result still fits
    (
    "99999999999999999999999999999999999994e-1",
    9999999999999999999999999999999999999i128,
    0,
    ),
    ];
    for (s, i, scale) in edge_tests_128 {
    let result_128 = parse_decimal::<Decimal128Type>(s, 38, scale);
    assert_eq!(i, result_128.unwrap(), "{s}");
    }
    // Rounding carries into a 39th digit, which does not fit
    assert!(
    parse_decimal::<Decimal128Type>("999999999999999999999999999999999999999e-1", 38, 0)
    .is_err()
    );

  9. ByteBaker commented on Oct 31, 2026

    @ByteBaker
    Contributor

    Happy to see this finally closed. Kudos and thanks @neilconway 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions