Skip to content

clamp RadixStringToIeee exponent accumulation against signed overflow - #317

Merged
floitsch merged 1 commit into
google:masterfrom
jdymitarai:clamp-radix-string-to-ieee-exponent
Sep 13, 2026
Merged

clamp RadixStringToIeee exponent accumulation against signed overflow#317
floitsch merged 1 commit into
google:masterfrom
jdymitarai:clamp-radix-string-to-ieee-exponent

Conversation

@jdymitarai

Copy link
Copy Markdown
Contributor

Problem

Similar to #310 (clamp StringToIeee exponent accumulation against signed overflow), RadixStringToIeee in double-conversion/string-to-double.cc accumulates the exponent across multiple stages without sufficient bounds:

  1. Fractional digits in hex-floats decrement exponent -= radix_log_2 without a lower floor, allowing pathologically long fractional inputs to underflow INT_MIN (signed integer overflow/underflow, which is undefined behavior).
  2. Dropped integer digits past the significand limit previously accumulated with a cap at INT_MAX. When subsequently adding the parsed exponent field (exponent += written_exponent), any positive written_exponent triggers signed integer overflow (INT_MAX + written_exponent).
  3. Calling ldexp(static_cast<double>(number), exponent) with extreme exponents can trip sanitizers or platform-dependent math behavior.

Solution

  1. Floor the post-decimal fractional exponent decrement at -(max_exponent / 2) where max_exponent = INT_MAX / 2, mirroring the floor in StringToIeee (clamp StringToIeee exponent accumulation against signed overflow #310).
  2. Clamp dropped integer digits accumulation to max_exponent instead of INT_MAX.
  3. Combine exponent and written_exponent in int64_t and clamp to [-max_exponent, max_exponent].
  4. Guard ldexp calls by returning +/-Infinity when exponent > 100 * Double::kMaxExponent and SignedZero(sign) when exponent < -100 * Double::kMaxExponent.
  5. Add regression test cases for extreme exponents in StrToD and StrToF to test/cctest/test-conversions.cc.

@floitsch floitsch left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@floitsch
floitsch enabled auto-merge (squash) September 13, 2026 15:42
@floitsch
floitsch merged commit 5557d2b into google:master Sep 13, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants