-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix convert processor conversion of string with leading zeros to integer #15557
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -205,7 +205,7 @@ func toString(value interface{}) (string, error) { | |||||
func toLong(value interface{}) (int64, error) { | ||||||
switch v := value.(type) { | ||||||
case string: | ||||||
return strconv.ParseInt(v, 0, 64) | ||||||
return strToInt(v, 64) | ||||||
case int: | ||||||
return int64(v), nil | ||||||
case int8: | ||||||
|
@@ -238,7 +238,7 @@ func toLong(value interface{}) (int64, error) { | |||||
func toInteger(value interface{}) (int32, error) { | ||||||
switch v := value.(type) { | ||||||
case string: | ||||||
i, err := strconv.ParseInt(v, 0, 32) | ||||||
i, err := strToInt(v, 32) | ||||||
return int32(i), err | ||||||
case int: | ||||||
return int32(v), nil | ||||||
|
@@ -403,3 +403,13 @@ func cloneValue(value interface{}) interface{} { | |||||
return value | ||||||
} | ||||||
} | ||||||
|
||||||
// Helper to interpret a string as either base-10 or base-16. | ||||||
func strToInt(v string, bitSize int) (int64, error) { | ||||||
base := 10 | ||||||
if strings.IndexAny(v, "xX") != -1 { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm wondering if we should check to see if prefix is "0x"||"0X"? It seems odd that we would assume 0x if we found an x anywhere in the string There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Checking for a 0x prefix will require to also account for an optional -+ sign first, so we will end up duplicating most of the parsing that strconv.ParseInt does. By just checking for an x and passing it to ParseInt, we are already sure that it's either an hex number or an not a number at all, so ParseInt will take care of that for us. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think a more strict check would be clearer. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @asr That makes sense then. How about just clearing up that comment below to also include There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess you meant @adriansr. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @asr I'm sorry. And sorry for all the times I'll probably accidentally do that in the future. 🤦♂ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in retrospect it was silly to use strings.IndexAny on the whole string for something so simple, which has a lot of overhead. I've ended up checking for the prefix as you said 😅 |
||||||
// strconv.ParseInt only accepts the '0x' prefix when base is 0. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||||||
base = 0 | ||||||
} | ||||||
return strconv.ParseInt(v, base, bitSize) | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: can you use the
strToInt blah blah
godoc format here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done