Skip to content

reject negative requested_digits in ToFixed - #298

Merged
floitsch merged 1 commit into
google:masterfrom
Ramya-9353:tofixed-negative-digits
Jul 28, 2026
Merged

reject negative requested_digits in ToFixed#298
floitsch merged 1 commit into
google:masterfrom
Ramya-9353:tofixed-negative-digits

Conversation

@Ramya-9353

Copy link
Copy Markdown
Contributor

Repro: ToFixed(1e-30, -1000, &builder) on a buffer sized by the header's own formula (1 + kMaxFixedDigitsBeforePoint + 1 + kMaxFixedDigitsAfterPoint + 1, so 163 bytes) returns true and writes 1000 characters; ASan reports a heap-buffer-overflow write in CreateDecimalRepresentation, and the length written is exactly -requested_digits.

Cause: requested_digits is bounded only from above, so a negative count reaches CreateDecimalRepresentation as decimal_point == -requested_digits and AddPadding emits that many characters. The lower bound existed only as the DOUBLE_CONVERSION_ASSERT in DoubleToAscii, which is compiled out under NDEBUG. ToFixed(1e-30, INT_MIN) additionally trips UBSan on the negation in fixed-dtoa.cc:389.

Fix: reject negative requested_digits up front, matching the guards ToExponential and ToPrecision already carry. Values 0 to kMaxFixedDigitsAfterPoint are unaffected.

@google-cla

google-cla Bot commented Jul 28, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@floitsch

Copy link
Copy Markdown
Collaborator

This is arguably something the caller should check and not do, but I'm in favor of catching it ourselves.
The patch is small enough that I will accept it without you signing the CLA. (That said: I recommend signing it, as it makes it easier to contribute to Google projects).
Thanks.

@floitsch
floitsch merged commit e2a882d into google:master Jul 28, 2026
8 of 9 checks passed
@Ramya-9353

Copy link
Copy Markdown
Contributor Author

Appreciate the merge, and thanks for taking it even without the CLA signed.

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