guard non-positive requested digits in DoubleToAscii and BignumDtoa - #318
Open
jdymitarai wants to merge 1 commit into
Open
guard non-positive requested digits in DoubleToAscii and BignumDtoa#318jdymitarai wants to merge 1 commit into
jdymitarai wants to merge 1 commit into
Conversation
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
Following the hardening of
DigitGenCounted(#313),GenerateCountedDigits(#314), andFastFixedDtoa(#315) against non-positive/negative counts, entry points and bignum utilities still had edge cases in release builds (-DNDEBUG):DoubleToStringConverter::DoubleToAscii:DoubleToAsciicheckedif (mode == PRECISION && requested_digits == 0)to return an empty representation (*length = 0,*point = 0).requested_digits < 0in release builds (whereDOUBLE_CONVERSION_ASSERTis compiled out), this check failed.FastDtoareturned false (guard non-positive requested_digits in DigitGenCounted #313), and execution proceeded toBignumDtoa, setting*length = 0(guard non-positive count in GenerateCountedDigits #314) but leaving*pointat an unreset, non-zero value, after doing redundant bignum work.mode == FIXEDwithrequested_digits < 0,DoubleToAsciifell through toBignumDtoa, whereBignumToFixedcould emit truncated integer digits intobufferwhen(*decimal_point) + requested_digits > 0.BignumDtoa:mode == BIGNUM_DTOA_PRECISION && requested_digits <= 0ormode == BIGNUM_DTOA_FIXED && requested_digits < 0,BignumDtoalacked early return guards.BignumShift and Exponent Operations:Bignum::ShiftLeft(shift_amount): ifshift_amount <= 0, non-positive shifts are a no-op, but previously calledBigitsShiftLeft(local_shift)which triggered undefined behavior on negative shifts.Bignum::MultiplyByPowerOfTen(exponent): checkedif (exponent == 0) return;. Forexponent < 0, it bypassed the multiplier loops and calledShiftLeft(exponent)with a negative shift amount.Bignum::AssignPowerUInt16(base, power_exponent): checkedif (power_exponent == 0) return;. Negative exponents bypassed the loops and calledShiftLeft(shifts * power_exponent).Solution
DoubleToStringConverter::DoubleToAscii, guard(mode == PRECISION && requested_digits <= 0) || (mode == FIXED && requested_digits < 0)by returning early with an empty string,*length = 0, and*point = 0.BignumDtoa, return early with an empty string,*length = 0, and*decimal_point = 0for(mode == BIGNUM_DTOA_PRECISION && requested_digits <= 0) || (mode == BIGNUM_DTOA_FIXED && requested_digits < 0).Bignum::ShiftLeft, return early ifshift_amount <= 0.Bignum::MultiplyByPowerOfTenandBignum::AssignPowerUInt16, change checks to<= 0.test/cctest/test-dtoa.cc,test/cctest/test-bignum-dtoa.cc, andtest/cctest/test-bignum.cc.