From 75243034b8f5425c51de27aa53e345a792ecd492 Mon Sep 17 00:00:00 2001 From: James Baker Date: Tue, 3 Dec 2024 15:32:47 +1100 Subject: [PATCH 1/2] fix: allow for empty files on hook check --- internal/job/hook/binary.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/internal/job/hook/binary.go b/internal/job/hook/binary.go index eae7b325b9..a15fe38618 100644 --- a/internal/job/hook/binary.go +++ b/internal/job/hook/binary.go @@ -60,6 +60,16 @@ func isBinaryExecutable(path string) (bool, error) { return false, fmt.Errorf("reading first four bytes of file %q: %w", path, err) } + fileInfo, err := f.Stat() + if err != nil { + return false, fmt.Errorf("stat file %q: %w", path, err) + } + + if fileInfo.Size() < 4 { + // there are less than four bytes in the file, there's nothing that we can do with it + return false, nil + } + if len(firstFour) < 4 { // there are less than four bytes in the file, there's nothing that we can do with it return false, nil From 8a5a6234453883d4a9d5c8e316d3d6eadce1eda7 Mon Sep 17 00:00:00 2001 From: James Baker Date: Wed, 4 Dec 2024 14:24:22 +1100 Subject: [PATCH 2/2] fix: moved check higher in function to allow early detection --- internal/job/hook/binary.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/internal/job/hook/binary.go b/internal/job/hook/binary.go index a15fe38618..dde59fb41a 100644 --- a/internal/job/hook/binary.go +++ b/internal/job/hook/binary.go @@ -54,11 +54,6 @@ func isBinaryExecutable(path string) (bool, error) { } defer f.Close() - r := bufio.NewReader(f) - firstFour, err := r.Peek(4) - if err != nil { - return false, fmt.Errorf("reading first four bytes of file %q: %w", path, err) - } fileInfo, err := f.Stat() if err != nil { @@ -66,10 +61,16 @@ func isBinaryExecutable(path string) (bool, error) { } if fileInfo.Size() < 4 { - // there are less than four bytes in the file, there's nothing that we can do with it + // there are less than four bytes in the file, we assume it is an empty file and there's nothing that we can do with it return false, nil } + r := bufio.NewReader(f) + firstFour, err := r.Peek(4) + if err != nil { + return false, fmt.Errorf("reading first four bytes of file %q: %w", path, err) + } + if len(firstFour) < 4 { // there are less than four bytes in the file, there's nothing that we can do with it return false, nil