From 4ebb6a99602f113ac6bc30982310bfd9d2662b26 Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Thu, 27 May 2021 10:08:03 +0200 Subject: [PATCH] address: fix stack overflow bug when printing error Commit 24e673ae introduced a bug that cause the Error() function to call itself recursively forever, causing a stack overflow. We explicitly cast the error to its base type to avoid the recursion and add a small test case that would've triggered the bug before. --- address.go | 4 ++-- address_test.go | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/address.go b/address.go index 243a393..8672497 100644 --- a/address.go +++ b/address.go @@ -23,7 +23,7 @@ import ( type UnsupportedWitnessVerError byte func (e UnsupportedWitnessVerError) Error() string { - return fmt.Sprintf("unsupported witness version: %#x", e) + return fmt.Sprintf("unsupported witness version: %#x", byte(e)) } // UnsupportedWitnessProgLenError describes an error where a segwit address @@ -31,7 +31,7 @@ func (e UnsupportedWitnessVerError) Error() string { type UnsupportedWitnessProgLenError int func (e UnsupportedWitnessProgLenError) Error() string { - return fmt.Sprintf("unsupported witness program length: %d", e) + return fmt.Sprintf("unsupported witness program length: %d", int(e)) } var ( diff --git a/address_test.go b/address_test.go index 61f0fa9..b6a990d 100644 --- a/address_test.go +++ b/address_test.go @@ -859,6 +859,14 @@ func TestAddresses(t *testing.T) { test.name) return } + } else { + // If there is an error, make sure we can print it + // correctly. + errStr := err.Error() + if errStr == "" { + t.Errorf("%v: error was non-nil but message is"+ + "empty: %v", test.name, err) + } } if !test.valid {