guard non-positive count in GenerateCountedDigits - #314
Merged
floitsch merged 1 commit intoSep 10, 2026
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
PR #306 guarded against
count == 0inGenerateCountedDigits, preventing an out-of-bounds write tobuffer[-1]. However, when called with a negative digit count (e.g.count < 0or negativerequested_digitsinBignumDtoawithBIGNUM_DTOA_PRECISION), in release builds (-DNDEBUG), the assertionDOUBLE_CONVERSION_ASSERT(count >= 0)is stripped and thecount == 0check evaluates to false.Execution then proceeds to line 309:
which writes to
buffer[count - 1]before the start of the buffer (e.g.buffer[-2]whencount == -1), causing memory corruption / buffer overflow.Solution
Change
if (count == 0)toif (count <= 0)inGenerateCountedDigitsto safely treat any non-positive digit count as requesting no digits (*length = 0). This mirrors the non-positive guard introduced inDigitGenCountedin PR #313.Extend
BignumDtoaZeroPrecisionintest/cctest/test-bignum-dtoa.ccto verify negative precision values.