Skip to content

Commit

Permalink
prog, map: add nil checks for value marshaller
Browse files Browse the repository at this point in the history
In case of programs or maps as map value types, the value marshalling
results in a panic in case of a nil pointer. For other types this is not a
problem since sysenc.Marshal does a nil check.

This commit adds a nil check to the internal helper for marshalling
programs and maps.

Signed-off-by: Tobias Böhm <[email protected]>
  • Loading branch information
aibor committed Nov 11, 2024
1 parent 9894d25 commit 7c8dd1d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
4 changes: 4 additions & 0 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -1589,6 +1589,10 @@ func unmarshalMap(buf sysenc.Buffer) (*Map, error) {

// marshalMap marshals the fd of a map into a buffer in host endianness.
func marshalMap(m *Map, length int) ([]byte, error) {
if m == nil {
return nil, errors.New("map cannot be nil")
}

if length != 4 {
return nil, fmt.Errorf("can't marshal map to %d bytes", length)
}
Expand Down
8 changes: 7 additions & 1 deletion map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1385,7 +1385,13 @@ func TestIterateMapInMap(t *testing.T) {
parent := createMapInMap(t, ArrayOfMaps)
defer parent.Close()

a := createArray(t)
var a *Map

if err := parent.Put(idx, a); err == nil {
t.Fatal("Put accepts nil map")
}

a = createArray(t)

if err := parent.Put(idx, a); err != nil {
t.Fatal(err)
Expand Down
4 changes: 4 additions & 0 deletions prog.go
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,10 @@ func unmarshalProgram(buf sysenc.Buffer) (*Program, error) {
}

func marshalProgram(p *Program, length int) ([]byte, error) {
if p == nil {
return nil, errors.New("program cannot be nil")
}

if length != 4 {
return nil, fmt.Errorf("can't marshal program to %d bytes", length)
}
Expand Down
8 changes: 7 additions & 1 deletion prog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,13 @@ func TestProgramMarshaling(t *testing.T) {
arr := createProgramArray(t)
defer arr.Close()

prog := mustSocketFilter(t)
var prog *Program

if err := arr.Put(idx, prog); err == nil {
t.Fatal("Put accepts nil program")
}

prog = mustSocketFilter(t)

if err := arr.Put(idx, prog); err != nil {
t.Fatal("Can't put program:", err)
Expand Down

0 comments on commit 7c8dd1d

Please sign in to comment.