Skip to content

Commit

Permalink
Fix handling of empty strings in UTF16BytesToString. (elastic#3705)
Browse files Browse the repository at this point in the history
* Fix handling of empty strings in UTF16BytesToString.

(cherry picked from commit 65b9385)
  • Loading branch information
ohadravid authored and andrewkroh committed Mar 2, 2017
1 parent a2ef3ae commit 0300146
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,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

0 comments on commit 0300146

Please sign in to comment.