Repository navigation
fix(number): reject bare dot and repeated plus in string-to-number conversions - #374
Open
fabrianivan wants to merge 1 commit into
Open
fabrianivan wants to merge 1 commit into
fabrianivan wants to merge 1 commit into
Conversation
…nversions
1. In trimDecimal, strings with only a dot or signed dot ('.', '+.', '-.')
previously matched stringNumberRe, capturing an empty integer part that was
then mapped to '0', '+0', or '-0', allowing ToIntE('.') to return 0 with no error
while ToFloat64E('.') failed. This change skips trimming for bare dot inputs so
strconv.ParseInt/ParseUint can properly reject them as invalid syntax.
2. In parseUint, strings.TrimLeft was used on '+', which inadvertently stripped
multiple plus signs (e.g. '++5' parsed as 5). Replaced with strings.TrimPrefix
so only a single optional plus sign is stripped.
|
|
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
trimDecimal, strings containing only a dot or signed dot (".","+.","-.") matchstringNumberRe(^([-+]?\d*)(\.\d*)?$). Because digits before and after the dot are both optional, the empty integer part is captured and mapped to"0","+0", or"-0". As a result,cast.ToIntE(".")returns0, nilwithout error, whilecast.ToFloat64E(".")andstrconv.ParseFloatfail with syntax errors.parseUint,strings.TrimLeft(..., "+")was used, which strips all leading plus signs. Consequently, invalid numeric strings like"++5"or"+++5"were parsed as5, nilinstead of being rejected.Solution
trimDecimal, immediately returnswhensis".","+.", or"-."sostrconv.ParseIntandstrconv.ParseUintcan reject it withstrconv.ErrSyntax.parseUint, usestrings.TrimPrefix(..., "+")instead ofstrings.TrimLeftso only a single optional leading plus sign is stripped.Verification
number_internal_test.goandnumber_test.go(TestInvalidNumberStrings) for".","+.","-.", and"++5".go test -v -race ./...andgo vet ./....