From 9e7f2cf9d54e2b79f74d0932f1661b3ec6ef70ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20R=C3=BCger?= Date: Fri, 13 Mar 2026 01:13:36 +0100 Subject: [PATCH] fix: check os.ReadFile error before using file contents http.DetectContentType was called with potentially nil/empty contents when ReadFile failed; the error was only checked afterward. Move the error check immediately after ReadFile so bad data is never used. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- page/link.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/page/link.go b/page/link.go index 0035a8f..758ecd8 100644 --- a/page/link.go +++ b/page/link.go @@ -102,6 +102,9 @@ func resolveLink( } linkContents, err := os.ReadFile(filepath) + if err != nil { + return "", karma.Format(err, "read file: %s", filepath) + } contentType := http.DetectContentType(linkContents) // Check if the MIME type starts with "text/" @@ -110,10 +113,6 @@ func resolveLink( return "", nil } - if err != nil { - return "", karma.Format(err, "read file: %s", filepath) - } - linkContents = bytes.ReplaceAll( linkContents, []byte("\r\n"),