Skip to content

Commit

Permalink
linters: add makezero and fix a related bug
Browse files Browse the repository at this point in the history
Makezero is a linter to ensure that slices are not initialized with
non-zero length. Since in Go, we most of the time use append on the
slice, initializing it with non-zero length can tend to create a slice
full of zero that will clobber the beginning of the slice.

In the bug fixed here, we created a list with 300+ zeros and then append
after them, only to skip the zero in the calling method. Usually we want
to prealloc the slice with a capacity of len but a length of 0, i.e.
replace make([]int, len(nums)) with make([]int, 0, len(nums)).  See more
info here https://github.com/ashanbrown/makezero.

Signed-off-by: Mahe Tardy <[email protected]>
  • Loading branch information
mtardy committed Dec 4, 2023
1 parent 3d1a5fe commit f5d4afd
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 5 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ linters:
- staticcheck
- unused
- gosimple
- makezero

linters-settings:
goheader:
Expand Down
4 changes: 0 additions & 4 deletions pkg/btf/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,10 +325,6 @@ func GetSyscallsList() ([]string, error) {
for _, key := range syscallinfo.SyscallsNames() {
var fn *btf.Func

if key == "" {
continue
}

sym, err := arch.AddSyscallPrefix(key)
if err != nil {
return []string{}, err
Expand Down
2 changes: 1 addition & 1 deletion pkg/syscallinfo/syscallinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var syscallIDs = func() map[string]int {
}()

func SyscallsNames() []string {
ret := make([]string, len(syscallNames))
ret := make([]string, 0, len(syscallNames))

for _, v := range syscallNames {
ret = append(ret, v)
Expand Down

0 comments on commit f5d4afd

Please sign in to comment.