Skip to content

Commit

Permalink
address: fix stack overflow bug when printing error
Browse files Browse the repository at this point in the history
Commit 24e673a 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.
  • Loading branch information
guggero committed May 27, 2021
1 parent 90ffd44 commit 4ebb6a9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions address.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ 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
// being decoded has an unsupported witness program length.
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 (
Expand Down
8 changes: 8 additions & 0 deletions address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 4ebb6a9

Please sign in to comment.