Skip to content

Commit 3562dab

Browse files
authored
fix(lua-rockspec): handle empty and whitespace-only rockspec files gracefully (#4827)
Empty or whitespace-only .rockspec files cause parseRockspecBlock to panic with "index out of range" because the existing end-of-data guard requires len(out) > 0 before returning the "unexpected end of block" error, letting the bare data[*i] access on the next line crash. Split the guard so that: - partial content at end of data still returns the existing error - empty data (or whitespace-only) returns an empty block cleanly Closes #4824. Signed-off-by: Akihiko Komada <aki1770@gmail.com>
1 parent 014a4c9 commit 3562dab

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

syft/pkg/cataloger/lua/rockspec_parser.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,11 @@ func parseRockspecBlock(data []byte, i *int, locals map[string]string) ([]rocksp
6363

6464
parsing.SkipWhitespace(data, i)
6565

66-
if *i >= len(data) && len(out) > 0 {
67-
return nil, fmt.Errorf("unexpected end of block at %d", *i)
66+
if *i >= len(data) {
67+
if len(out) > 0 {
68+
return nil, fmt.Errorf("unexpected end of block at %d", *i)
69+
}
70+
return out, nil
6871
}
6972

7073
c := data[*i]

syft/pkg/cataloger/lua/rockspec_parser_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ func Test_parseRockspecData(t *testing.T) {
1414
content string
1515
wantErr require.ErrorAssertionFunc
1616
}{
17+
{
18+
name: "empty file",
19+
content: ``,
20+
},
21+
{
22+
name: "whitespace only",
23+
content: `
24+
25+
26+
`,
27+
},
1728
{
1829
name: "basic valid content",
1930
content: `

0 commit comments

Comments
 (0)