Skip to content
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

Merged
merged 4 commits into from
Jan 15, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.next.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Fix bug with potential concurrent reads and writes from event.Meta map by Kafka output. {issue}14542[14542] {pull}14568[14568]
- Fix spooling to disk blocking infinitely if the lock file can not be acquired. {pull}15338[15338]
- Fix `metricbeat test output` with an ipv6 ES host in the output.hosts. {pull}15368[15368]
- Fix `convert` processor conversion of string to integer with leading zeros. {issue}15513[15513] {pull}15557[15557]

*Auditbeat*

Expand Down
14 changes: 12 additions & 2 deletions libbeat/processors/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -403,3 +403,13 @@ func cloneValue(value interface{}) interface{} {
return value
}
}

// Helper to interpret a string as either base-10 or base-16.
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

func strToInt(v string, bitSize int) (int64, error) {
base := 10
if strings.IndexAny(v, "xX") != -1 {
Copy link
Contributor

Choose a reason for hiding this comment

The 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

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a more strict check would be clearer.

Copy link
Member

Choose a reason for hiding this comment

The 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 0X (I was reading that in a very strict sense).

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you meant @adriansr.

Copy link
Member

Choose a reason for hiding this comment

The 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. 🤦‍♂ slack handle != github handle

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// strconv.ParseInt only accepts the '0x' prefix when base is 0.
// strconv.ParseInt will accept the '0x' or '0X` prefix only when base is 0.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

base = 0
}
return strconv.ParseInt(v, base, bitSize)
}
19 changes: 19 additions & 0 deletions libbeat/processors/convert/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,16 @@ var testCases = []testCase{

{Long, nil, nil, true},
{Long, "x", nil, true},
{Long, "0x", nil, true},
{Long, "0b1", nil, true},
{Long, "1x2", nil, true},
{Long, true, nil, true},
{Long, "1", int64(1), false},
{Long, "-1", int64(-1), false},
{Long, "017", int64(17), false},
{Long, "08", int64(8), false},
{Long, "0X0A", int64(10), false},
{Long, "-0x12", int64(-18), false},
{Long, int(1), int64(1), false},
{Long, int8(1), int64(1), false},
{Long, int16(1), int64(1), false},
Expand All @@ -294,6 +302,17 @@ var testCases = []testCase{
{Integer, nil, nil, true},
{Integer, "x", nil, true},
{Integer, true, nil, true},
{Integer, "x", nil, true},
{Integer, "0x", nil, true},
{Integer, "0b1", nil, true},
{Integer, "1x2", nil, true},
{Integer, true, nil, true},
{Integer, "1", int32(1), false},
{Integer, "-1", int32(-1), false},
{Integer, "017", int32(17), false},
{Integer, "08", int32(8), false},
{Integer, "0X0A", int32(10), false},
{Integer, "-0x12", int32(-18), false},
{Integer, "1", int32(1), false},
{Integer, int(1), int32(1), false},
{Integer, int8(1), int32(1), false},
Expand Down