From 91f01b0df7a1561b29f7c358e0634a28b0690f21 Mon Sep 17 00:00:00 2001 From: Sebastian Stammler Date: Fri, 26 Jan 2024 20:49:15 +0100 Subject: [PATCH] common: Let Hex2Bytes and Hex2BytesFixed panic for invalid hex strings --- common/bytes.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/common/bytes.go b/common/bytes.go index d1f5c6c995..3fb05da750 100644 --- a/common/bytes.go +++ b/common/bytes.go @@ -76,14 +76,24 @@ func Bytes2Hex(d []byte) string { } // Hex2Bytes returns the bytes represented by the hexadecimal string str. +// The string must not be prefixed with `0x`. +// It panics if str contains a non-hex char. func Hex2Bytes(str string) []byte { - h, _ := hex.DecodeString(str) + h, err := hex.DecodeString(str) + if err != nil { + panic("error decoding string: " + err.Error()) + } return h } // Hex2BytesFixed returns bytes of a specified fixed length flen. +// The string must not be prefixed with `0x`. +// It panics if str contains a non-hex char. func Hex2BytesFixed(str string, flen int) []byte { - h, _ := hex.DecodeString(str) + h, err := hex.DecodeString(str) + if err != nil { + panic("error decoding string: " + err.Error()) + } if len(h) == flen { return h }