Browse Source

PubKeyFromBytes: return zero value PubKey on error

Fixes https://github.com/tendermint/go-crypto/issues/48.

This previously skewed up my fuzzing tests so ensure
that on error we return the zero value PubKey.
pull/1782/head
Emmanuel Odeke 7 years ago
parent
commit
b0cf4b4757
No known key found for this signature in database GPG Key ID: 1CA47A292F89DD40
2 changed files with 11 additions and 2 deletions
  1. +4
    -2
      pub_key.go
  2. +7
    -0
      pub_key_test.go

+ 4
- 2
pub_key.go View File

@ -14,8 +14,10 @@ import (
)
func PubKeyFromBytes(pubKeyBytes []byte) (pubKey PubKey, err error) {
err = wire.ReadBinaryBytes(pubKeyBytes, &pubKey)
return
if err := wire.ReadBinaryBytes(pubKeyBytes, &pubKey); err != nil {
return PubKey{}, err
}
return pubKey, nil
}
//----------------------------------------


+ 7
- 0
pub_key_test.go View File

@ -6,6 +6,7 @@ import (
"github.com/btcsuite/btcutil/base58"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type keyData struct {
@ -39,3 +40,9 @@ func TestPubKeySecp256k1Address(t *testing.T) {
assert.Equal(t, addr, addrB, "Expected addresses to match")
}
}
func TestPubKeyInvalidDataProperReturnsEmpty(t *testing.T) {
pk, err := PubKeyFromBytes([]byte("foo"))
require.NotNil(t, err, "expecting a non-nil error")
require.True(t, pk.Empty(), "expecting an empty public key on error")
}

Loading…
Cancel
Save