Skip to content

Commit 41958b7

Browse files
therealak12lmb
authored andcommitted
btf: accept empty string table
Fixes: #1818 Signed-off-by: therealak12 <[email protected]> Signed-off-by: Lorenz Bauer <[email protected]>
1 parent 59d5f32 commit 41958b7

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

btf/btf_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package btf
22

33
import (
44
"bytes"
5+
"encoding/binary"
56
"errors"
67
"fmt"
78
"io/fs"
@@ -536,6 +537,23 @@ func TestSpecConcurrentAccess(t *testing.T) {
536537
wg.Wait()
537538
}
538539

540+
func TestLoadEmptyRawSpec(t *testing.T) {
541+
buf, err := binary.Append(nil, binary.LittleEndian, &btfHeader{
542+
Magic: btfMagic,
543+
Version: 1,
544+
Flags: 0,
545+
HdrLen: uint32(btfHeaderLen),
546+
TypeOff: 0,
547+
TypeLen: 0,
548+
StringOff: 0,
549+
StringLen: 0,
550+
})
551+
qt.Assert(t, qt.IsNil(err))
552+
553+
_, err = loadRawSpec(buf, nil)
554+
qt.Assert(t, qt.IsNil(err))
555+
}
556+
539557
func BenchmarkSpecCopy(b *testing.B) {
540558
spec := vmlinuxTestdataSpec(b)
541559
b.ResetTimer()

btf/strings.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"sync"
1111
)
1212

13-
// stringTable is contains a sequence of null-terminated strings.
13+
// stringTable contains a sequence of null-terminated strings.
1414
//
1515
// It is safe for concurrent use.
1616
type stringTable struct {
@@ -44,16 +44,14 @@ func newStringTable(bytes []byte, base *stringTable) (*stringTable, error) {
4444
firstStringOffset = uint32(len(base.bytes))
4545
}
4646

47-
if len(bytes) == 0 {
48-
return nil, errors.New("string table is empty")
49-
}
50-
51-
if bytes[len(bytes)-1] != 0 {
52-
return nil, errors.New("string table isn't null terminated")
53-
}
47+
if len(bytes) > 0 {
48+
if bytes[len(bytes)-1] != 0 {
49+
return nil, errors.New("string table isn't null terminated")
50+
}
5451

55-
if firstStringOffset == 0 && bytes[0] != 0 {
56-
return nil, errors.New("first item in string table is non-empty")
52+
if firstStringOffset == 0 && bytes[0] != 0 {
53+
return nil, errors.New("first item in string table is non-empty")
54+
}
5755
}
5856

5957
return &stringTable{base: base, bytes: bytes}, nil

btf/strings_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ func TestStringTable(t *testing.T) {
6262
}
6363
}
6464

65+
func TestEmptyStringTable(t *testing.T) {
66+
empty, err := newStringTable(nil, nil)
67+
qt.Assert(t, qt.IsNil(err))
68+
str, err := empty.Lookup(0)
69+
qt.Assert(t, qt.IsNil(err), qt.Commentf("Can't lookup empty string"))
70+
qt.Assert(t, qt.Equals(str, ""), qt.Commentf("Empty string lookup returned %q", str))
71+
_, err = empty.Lookup(1)
72+
qt.Assert(t, qt.IsNotNil(err))
73+
}
74+
6575
func TestStringTableBuilder(t *testing.T) {
6676
stb := newStringTableBuilder(0)
6777

0 commit comments

Comments
 (0)