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.
This commit is contained in:
Oliver Gugger 2021-05-27 10:08:03 +02:00
parent 90ffd449a4
commit 4ebb6a9960
No known key found for this signature in database
GPG key ID: 8E4256593F177720
2 changed files with 10 additions and 2 deletions

View file

@ -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 (

View file

@ -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 {