From 3562dab445a4708abd945e8305e74288c2a7564f Mon Sep 17 00:00:00 2001 From: Akihiko Komada Date: Sat, 25 Apr 2026 01:44:25 +0900 Subject: [PATCH] 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 --- syft/pkg/cataloger/lua/rockspec_parser.go | 7 +++++-- syft/pkg/cataloger/lua/rockspec_parser_test.go | 11 +++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/syft/pkg/cataloger/lua/rockspec_parser.go b/syft/pkg/cataloger/lua/rockspec_parser.go index 89c730923..56c9a87f2 100644 --- a/syft/pkg/cataloger/lua/rockspec_parser.go +++ b/syft/pkg/cataloger/lua/rockspec_parser.go @@ -63,8 +63,11 @@ func parseRockspecBlock(data []byte, i *int, locals map[string]string) ([]rocksp parsing.SkipWhitespace(data, i) - if *i >= len(data) && len(out) > 0 { - return nil, fmt.Errorf("unexpected end of block at %d", *i) + if *i >= len(data) { + if len(out) > 0 { + return nil, fmt.Errorf("unexpected end of block at %d", *i) + } + return out, nil } c := data[*i] diff --git a/syft/pkg/cataloger/lua/rockspec_parser_test.go b/syft/pkg/cataloger/lua/rockspec_parser_test.go index 63cbde502..0914d2695 100644 --- a/syft/pkg/cataloger/lua/rockspec_parser_test.go +++ b/syft/pkg/cataloger/lua/rockspec_parser_test.go @@ -14,6 +14,17 @@ func Test_parseRockspecData(t *testing.T) { content string wantErr require.ErrorAssertionFunc }{ + { + name: "empty file", + content: ``, + }, + { + name: "whitespace only", + content: ` + + +`, + }, { name: "basic valid content", content: `