From 5b030628ddf1db3db0dd013a0238a92b3d74bdde Mon Sep 17 00:00:00 2001 From: Gijs van Dam Date: Fri, 10 Oct 2025 13:26:24 +0200 Subject: [PATCH] btclog: handle nil slices in HexN gracefully With this commit HexN now checks if the provided byte slice is nil. If it is, it returns a slog attribute with the string "". This makes it easier to search for nil slices in the logs. --- v2/attrs.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/v2/attrs.go b/v2/attrs.go index 5a0fee9..471aa25 100644 --- a/v2/attrs.go +++ b/v2/attrs.go @@ -32,6 +32,11 @@ func Hex2(key string, value []byte) slog.Attr { // HexN is a convenience function for hex-encoded log attributes which prints // a maximum of n bytes. func HexN(key string, value []byte, n uint) slog.Attr { + // Handle nil slice gracefully. + if value == nil { + return slog.String(key, "") + } + if len(value) <= int(n) { return slog.String(key, hex.EncodeToString(value)) }