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

Regression: Fix assigning to nil map in DeepMerge #18143

Merged
merged 3 commits into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -80,6 +80,7 @@ https://github.com/elastic/beats/compare/v7.0.0-alpha2...master[Check the HEAD d
- Fix goroutine leak and Elasticsearch output file descriptor leak when output reloading is in use. {issue}10491[10491] {pull}17381[17381]
- Fix `setup.dashboards.index` setting not working. {pull}17749[17749]
- Fix Elasticsearch license endpoint URL referenced in error message. {issue}17880[17880] {pull}18030[18030]
- Fix panic when assigning a key to a `nil` value in an event. {pull}18143[18143]

*Auditbeat*

Expand Down
16 changes: 11 additions & 5 deletions libbeat/common/mapstr.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,26 @@ func (m MapStr) deepUpdateMap(d MapStr, overwrite bool) {
}

func deepUpdateValue(old interface{}, val MapStr, overwrite bool) interface{} {
if old == nil {
return val
}

switch sub := old.(type) {
case MapStr:
if sub == nil {
return val
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Surprised that this isn't caught already by the if old == nil check at the start of this function. 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

Just found https://groups.google.com/forum/#!topic/golang-nuts/wnH302gBa4I/discussion.

Should we remove the code on lines 99-101?

Copy link
Author

Choose a reason for hiding this comment

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

There is nil, and then there is nil :)

An interface value is a tuple (type, value). An interface is nil, if and only if both type and value are nil. This is why the initial check fails here. We have a type (type MapStr, nil). The switch statement only looks for type, unpacks it, and we finally have the nil value in sub. This is when the call panics.

Removing line 99 should be safe. E.g.: https://play.golang.org/p/uajkDxQW9qH


sub.deepUpdateMap(val, overwrite)
return sub
case map[string]interface{}:
if sub == nil {
return val
}

tmp := MapStr(sub)
tmp.deepUpdateMap(val, overwrite)
return tmp
default:
// This should never happen
// We reach the default branch if old is no map or if old == nil.
// In either case we return `val`, such that the old value is completely
// replaced when merging.
return val
}
}
Expand Down
5 changes: 5 additions & 0 deletions libbeat/common/mapstr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ func TestMapStrDeepUpdate(t *testing.T) {
MapStr{"a.b": 1},
MapStr{"a": 1, "a.b": 1},
},
{
MapStr{"a": (MapStr)(nil)},
MapStr{"a": MapStr{"b": 1}},
MapStr{"a": MapStr{"b": 1}},
},
}

for i, test := range tests {
Expand Down