Skip to content

Commit b4c44cc

Browse files
committed
fix(sys,etw): Correct string parsing functions
1 parent 80a6b2d commit b4c44cc

File tree

2 files changed

+38
-26
lines changed

2 files changed

+38
-26
lines changed

pkg/sys/etw/types.go

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,14 @@ type ClassicEventID struct {
555555
_ [7]uint8 // reserved
556556
}
557557

558+
// EventFilterDescriptor defines the filter data that
559+
// a session passes to the provider's enable callback.
560+
type EventFilterDescriptor struct {
561+
Ptr uintptr
562+
Size uint32
563+
Type uint32
564+
}
565+
558566
// NewClassicEventID creates a new instance of classic event identifier.
559567
func NewClassicEventID(guid windows.GUID, typ uint16) ClassicEventID {
560568
return ClassicEventID{GUID: guid, Type: uint8(typ)}
@@ -604,7 +612,7 @@ func (e *EventRecord) ReadBytes(offset uint16, count uint16) []byte {
604612
if offset > e.BufferLen {
605613
return nil
606614
}
607-
return (*[1<<30 - 1]byte)(unsafe.Pointer(e.Buffer + uintptr(offset) + uintptr(count)))[:count:count]
615+
return (*[1<<30 - 1]byte)(unsafe.Pointer(e.Buffer + uintptr(offset)))[:count:count]
608616
}
609617

610618
// ReadUint16 reads the uint16 value from the buffer at the specified offset.
@@ -637,6 +645,7 @@ func (e *EventRecord) ReadAnsiString(offset uint16) (string, uint16) {
637645
if offset > e.BufferLen {
638646
return "", 0
639647
}
648+
640649
b := make([]byte, e.BufferLen)
641650
var i uint16
642651
for i < e.BufferLen {
@@ -647,49 +656,52 @@ func (e *EventRecord) ReadAnsiString(offset uint16) (string, uint16) {
647656
b[i] = c
648657
i++
649658
}
659+
660+
if i == 0 {
661+
return "", offset + 1
662+
}
663+
650664
if int(i) > len(b) {
651-
return string(b[:len(b)-1]), uint16(len(b))
665+
return string(b[:len(b)-1]), uint16(len(b)) + offset
652666
}
653-
return string(b[:i]), i + 1
667+
668+
return string(b[:i]), i + 1 + offset
654669
}
655670

656671
// ReadUTF16String reads the UTF-16 string from the buffer at the specified offset.
657-
// Returns the UTF-8 string and the number of bytes read from the string.
672+
// Returns the UTF-8 string and the number of bytes read from the string + the offset.
658673
func (e *EventRecord) ReadUTF16String(offset uint16) (string, uint16) {
659674
if offset > e.BufferLen {
660675
return "", 0
661676
}
662677

678+
// we're reading the leading string. First, calculate
679+
// the length of the null-terminated UTF16 string
680+
i := offset
663681
var length uint16
664-
665-
if offset > 0 {
666-
length = e.BufferLen - offset
667-
} else {
668-
// we're reading the leading string. First, calculate
669-
// the length of the null-terminated UTF16 string
670-
var i uint16
671-
for i < e.BufferLen {
672-
c := *(*uint16)(unsafe.Pointer(e.Buffer + uintptr(i)))
673-
if c == 0 {
674-
break // null terminator
675-
}
676-
length += 2
677-
i += 2
682+
for i < e.BufferLen {
683+
c := *(*uint16)(unsafe.Pointer(e.Buffer + uintptr(i)))
684+
if c == 0 {
685+
break // null terminator
678686
}
687+
length += 2
688+
i += 2
679689
}
680690

681-
s := (*[1<<30 - 1]uint16)(unsafe.Pointer(e.Buffer + uintptr(offset)))[:length:length]
682-
if offset > 0 {
683-
return utf16.Decode(s[:len(s)/2-1-2]), uint16(len(s) + 2)
691+
if length == 0 {
692+
return "", offset + 2 // null terminator size
684693
}
685694

686-
return utf16.Decode(s[:len(s)/2]), uint16(len(s) + 2)
695+
b := (*[1<<30 - 1]uint16)(unsafe.Pointer(e.Buffer + uintptr(offset)))[:length:length]
696+
s := b[:len(b)/2]
697+
698+
return utf16.Decode(s), uint16((len(s)+1)*2) + offset
687699
}
688700

689701
// ReadNTUnicodeString reads the native Unicode string at the given offset.
690702
func (e *EventRecord) ReadNTUnicodeString(offset uint16) (string, uint16) {
691703
if offset > e.BufferLen {
692-
return "", offset
704+
return "", 0
693705
}
694706

695707
i := offset
@@ -704,7 +716,7 @@ func (e *EventRecord) ReadNTUnicodeString(offset uint16) (string, uint16) {
704716
}
705717

706718
if length == 0 {
707-
return "", offset
719+
return "", offset + 2 // null terminator size
708720
}
709721

710722
b := (*[1<<30 - 1]byte)(unsafe.Pointer(e.Buffer + uintptr(offset)))[:length:length]
@@ -715,7 +727,7 @@ func (e *EventRecord) ReadNTUnicodeString(offset uint16) (string, uint16) {
715727
Buffer: (*uint16)(unsafe.Pointer(&b[0])),
716728
}
717729

718-
return s.String(), offset + s.Length
730+
return s.String(), s.Length + offset
719731
}
720732

721733
// ConsumeUTF16String reads the byte slice with UTF16-encoded string

pkg/sys/etw/types_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func TestReadBuffer(t *testing.T) {
8484

8585
name, noffset := ev.ReadAnsiString(offset)
8686
assert.Equal(t, "cmd.exe", name)
87-
cmdline, _ := ev.ReadUTF16String(noffset + offset)
87+
cmdline, _ := ev.ReadUTF16String(noffset)
8888
assert.Equal(t, "C:\\WINDOWS\\system32\\cmd.exe /c dir /-C /W \"\\\\?\\c:\\Users\\nedo\\AppData\\Roaming\\RabbitMQ\\db\\rabbit@archrabbit-mnesia\"", cmdline)
8989
},
9090
},

0 commit comments

Comments
 (0)