Skip to content

printf aborts on a hex float with a large-negative binary exponent (0x1p-9223372036854775808) #14835

Closed

Description

@leeewee

Parsing a hexadecimal floating-point argument whose binary exponent is a large-magnitude negative i64 (e.g. 0x1p-9223372036854775808) overflows bigdecimal's internal i64 scale inside BigDecimal::powi. uutils reaches it from num_parser.rs (base.powi(exponent)), directly under a comment asserting "We cannot overflow/underflow BigDecimal here" — which is false. The exponent is bounded to fit i64 (via to_i64()), but a value near i64::MIN still makes powi accumulate a scale outside i64 range. It is an arithmetic-overflow panic: it aborts a debug / -C overflow-checks=on build (exit 101/134) and wraps silently in a stock --release build.

$ printf '%f' 0x1p-9223372036854775808
thread 'main' panicked at bigdecimal-0.4.10/src/arithmetic/multiplication.rs:648:19:
attempt to add with overflow
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
$ echo $?
101

Root cause

// src/uucore/src/lib/features/parser/num_parser.rs  (hex-float branch)
// powi "only" supports i64 values. Just overflow/underflow if the value provided
// is > 2**64 or < 2**-64.
let Some(exponent) = exponent.to_i64() else {
    return Err(make_error(exponent.is_positive(), negative));
};
let base: BigDecimal = 2.into();
// Note: We cannot overflow/underflow BigDecimal here, as we will not be able to
// reach the maximum/minimum scale (i64 range).
let pow2 = base.powi(exponent);      // <- overflows for large-negative exponent

The to_i64() guard only rejects exponents that do not fit i64; it does not bound the magnitude, and the comment's assumption that a within-i64 exponent cannot overflow the scale is wrong — powi(i64::MIN) builds a BigDecimal whose scale accumulation exceeds i64.

Activity

  1. added a commit that references this issue on Sep 24, 2026
    cd5a0ca
  2. added a commit that references this issue on Nov 21, 2026
    dded1b5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions