Skip to content

Commit

Permalink
feat(helpers): robust int and uint arg helpers
Browse files Browse the repository at this point in the history
Add a new uint argument helpers, which extracts a data argument from an
event by name and returns it if it was one of the uint types.
Additionally, add further checks for int64 and int types in the already
existing int argument helper.
  • Loading branch information
NDStrahilevitz authored and geyslan committed Oct 9, 2024
1 parent 4d5e47d commit 0b23713
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions signatures/helpers/arguments_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,44 @@ func GetTraceeIntArgumentByName(event trace.Event, argName string) (int, error)
if err != nil {
return 0, err
}
argInt, ok := arg.Value.(int32)

argInt32, ok := arg.Value.(int32)
if ok {
return int(argInt32), nil
}
argInt64, ok := arg.Value.(int64)
if ok {
return int(argInt64), nil
}
argInt, ok := arg.Value.(int)
if ok {
return argInt, nil
}

return 0, fmt.Errorf("can't convert argument %v to int (argument is of type %T)", argName, arg.Value)
}

// GetTraceeUIntArgumentByName gets the argument matching the "argName" given from the event "argv" field, casted as int.
func GetTraceeUintArgumentByName(event trace.Event, argName string) (uint, error) {
arg, err := GetTraceeArgumentByName(event, argName, GetArgOps{DefaultArgs: false})
if err != nil {
return 0, err
}

argUint32, ok := arg.Value.(uint32)
if ok {
return uint(argUint32), nil
}
argUint64, ok := arg.Value.(uint64)
if ok {
return uint(argUint64), nil
}
argUint, ok := arg.Value.(uint)
if ok {
return int(argInt), nil
return argUint, nil
}

return 0, fmt.Errorf("can't convert argument %v to int", argName)
return 0, fmt.Errorf("can't convert argument %v to int (argument is of type %T)", argName, arg.Value)
}

// GetTraceeSliceStringArgumentByName retrieves the argument from the event's "Args" field
Expand Down

0 comments on commit 0b23713

Please sign in to comment.