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 handling of empty strings in UTF16BytesToString. #3705

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.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ https://github.com/elastic/beats/compare/v5.1.1...master[Check the HEAD diff]

*Winlogbeat*

- Fix handling of empty strings in event_data. {pull}3705[3705]

==== Added

Expand Down
2 changes: 1 addition & 1 deletion winlogbeat/sys/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func UTF16BytesToString(b []byte) (string, int, error) {
offset := -1

// Find the null terminator if it exists and re-slice the b.
if nullIndex := indexNullTerminator(b); nullIndex > 0 {
if nullIndex := indexNullTerminator(b); nullIndex > -1 {
if len(b) > nullIndex+2 {
offset = nullIndex + 2
}
Expand Down
19 changes: 19 additions & 0 deletions winlogbeat/sys/strings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,25 @@ func TestUTF16BytesToStringOffset(t *testing.T) {
assert.Equal(t, -1, offset)
}

func TestUTF16BytesToStringOffsetWithEmptyString(t *testing.T) {
in := bytes.Join([][]byte{toUTF16Bytes(""), toUTF16Bytes("two")}, []byte{0, 0})

output, offset, err := UTF16BytesToString(in)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, "", output)
assert.Equal(t, 2, offset)

in = in[offset:]
output, offset, err = UTF16BytesToString(in)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, "two", output)
assert.Equal(t, -1, offset)
}

func BenchmarkUTF16BytesToString(b *testing.B) {
utf16Bytes := toUTF16Bytes("A logon was attempted using explicit credentials.")

Expand Down