Repository navigation
fix: parse leading-zero decimal strings like 08 correctly - #378
Open
abu-tahir-0 wants to merge 1 commit into
Open
abu-tahir-0 wants to merge 1 commit into
abu-tahir-0 wants to merge 1 commit into
Conversation
strconv.ParseInt/ParseUint with base 0 treat a leading zero as octal, so decimal strings such as "08" or "018" fail to parse and cast returns 0. Fall back to base 10 when base-0 parsing fails, preserving hex/binary/octal literals that already parse successfully.
|
|
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
cast.ToInt("08")andcast.ToUint("09")return0instead of8and9. This affects any zero-padded decimal string containing 8 or 9 (issue #214).Root Cause
parseInt/parseUintcallstrconv.ParseInt/ParseUintwithbase 0, which auto-detects a leading0as octal."08"/"09"are invalid octal, so parsing fails andcastreturns the zero value.strconv.Atoi(base 10) handles these correctly.Solution
Fall back to
base 10whenbase 0parsing fails. Inputs that already parse under base 0 (hex0xff, binary0b1010, legitimate octal010) keep their exact current behavior; only previously-erroring strings change. Applied to bothparseIntandparseUint.Testing
TestToIntLeadingZeroDecimalFallbackinnumber_test.gocoveringToInt,ToInt64, andToUintfor"08","09","018","019".go test -count=1 ./...andgo vet ./...pass.Impact
Zero-padded decimal strings are converted correctly instead of silently becoming zero.