Skip to content

Commit

Permalink
Fix buffer length check and add test cases
Browse files Browse the repository at this point in the history
Signed-off-by: grantseltzer <[email protected]>
  • Loading branch information
grantseltzer committed Dec 21, 2024
1 parent 44dd831 commit f0b61fb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
13 changes: 7 additions & 6 deletions pkg/dynamicinstrumentation/eventparser/event_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,20 @@ func parseParamValue(definition *ditypes.Param, buffer []byte) (*ditypes.Param,
}

valueStack := newParamStack()
for bufferIndex+8 < len(buffer) {
for bufferIndex <= len(buffer) {
paramDefinition := definitionStack.pop()
if paramDefinition == nil {
break
}
if reflect.Kind(paramDefinition.Kind) == reflect.String {
if len(buffer) > bufferIndex+int(paramDefinition.Size) {
paramDefinition.ValueStr = string(buffer[bufferIndex : bufferIndex+int(paramDefinition.Size)])
bufferIndex += int(paramDefinition.Size)
if bufferIndex+int(paramDefinition.Size) > len(buffer) {
break
}
paramDefinition.ValueStr = string(buffer[bufferIndex : bufferIndex+int(paramDefinition.Size)])
bufferIndex += int(paramDefinition.Size)
valueStack.push(paramDefinition)
} else if !isTypeWithHeader(paramDefinition.Kind) {
if bufferIndex+int(paramDefinition.Size) >= len(buffer) {
if bufferIndex+int(paramDefinition.Size) > len(buffer) {
break
}
// This is a regular value (no sub-fields).
Expand All @@ -113,7 +114,7 @@ func parseParamValue(definition *ditypes.Param, buffer []byte) (*ditypes.Param,
bufferIndex += int(paramDefinition.Size)
valueStack.push(paramDefinition)
} else if reflect.Kind(paramDefinition.Kind) == reflect.Pointer {
if bufferIndex+int(paramDefinition.Size) >= len(buffer) {
if bufferIndex+int(paramDefinition.Size) > len(buffer) {
break
}
paramDefinition.Fields = append(paramDefinition.Fields, valueStack.pop())
Expand Down
22 changes: 22 additions & 0 deletions pkg/dynamicinstrumentation/eventparser/event_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,28 @@ func TestParseParamValue(t *testing.T) {
},
},
},
{
name: "same sized string",
inputBuffer: []byte{
65, 65, 65,
},
inputDefinition: &ditypes.Param{
Type: "string", Size: 0x3, Kind: 0x18,
},
expectedValue: &ditypes.Param{
ValueStr: "AAA", Type: "string", Size: 0x3, Kind: 0x18,
},
},
{
name: "empty string",
inputBuffer: []byte{},
inputDefinition: &ditypes.Param{
Type: "string", Size: 0x0, Kind: 0x18,
},
expectedValue: &ditypes.Param{
ValueStr: "", Type: "string", Size: 0x0, Kind: 0x18,
},
},
}

for _, tt := range tests {
Expand Down

0 comments on commit f0b61fb

Please sign in to comment.