Skip to content

guard non-positive count in GenerateCountedDigits - #314

Merged
floitsch merged 1 commit into
google:masterfrom
jdymitarai:bignum-counted-digits-nonpositive
Sep 10, 2026
Merged

guard non-positive count in GenerateCountedDigits#314
floitsch merged 1 commit into
google:masterfrom
jdymitarai:bignum-counted-digits-nonpositive

Conversation

@jdymitarai

@jdymitarai jdymitarai commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

Problem

PR #306 guarded against count == 0 in GenerateCountedDigits, preventing an out-of-bounds write to buffer[-1]. However, when called with a negative digit count (e.g. count < 0 or negative requested_digits in BignumDtoa with BIGNUM_DTOA_PRECISION), in release builds (-DNDEBUG), the assertion DOUBLE_CONVERSION_ASSERT(count >= 0) is stripped and the count == 0 check evaluates to false.

Execution then proceeds to line 309:

buffer[count - 1] = static_cast<char>(digit + '0');

which writes to buffer[count - 1] before the start of the buffer (e.g. buffer[-2] when count == -1), causing memory corruption / buffer overflow.

Solution

Change if (count == 0) to if (count <= 0) in GenerateCountedDigits to safely treat any non-positive digit count as requesting no digits (*length = 0). This mirrors the non-positive guard introduced in DigitGenCounted in PR #313.

Extend BignumDtoaZeroPrecision in test/cctest/test-bignum-dtoa.cc to verify negative precision values.

PR google#306 added a guard for zero requested digits in GenerateCountedDigits, preventing an out-of-bounds write to buffer[-1]. However, in release builds (-DNDEBUG), non-positive values (count < 0) bypassed the count == 0 check, writing to buffer[count - 1] before the start of the buffer.

Change the check to count <= 0 to safely reject any non-positive digit counts and extend the test in test-bignum-dtoa.cc to verify negative counts.

@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 merged commit ce8bb53 into google:master Sep 10, 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