diff --git a/pkg/addressbook/addressbook.go b/pkg/addressbook/addressbook.go index 32b14e0a771..0010c6559b0 100644 --- a/pkg/addressbook/addressbook.go +++ b/pkg/addressbook/addressbook.go @@ -82,6 +82,9 @@ func (s *store) Get(overlay swarm.Address) (*bzz.Address, bool, error) { } return nil, false, err } + if v.Address == nil { + return nil, false, ErrNotFound + } return v.Address, v.Verified, nil } diff --git a/pkg/addressbook/addressbook_test.go b/pkg/addressbook/addressbook_test.go index f515527d21e..36dac87fe51 100644 --- a/pkg/addressbook/addressbook_test.go +++ b/pkg/addressbook/addressbook_test.go @@ -5,6 +5,7 @@ package addressbook_test import ( + "encoding/json" "errors" "testing" @@ -13,6 +14,7 @@ import ( "github.com/ethersphere/bee/v2/pkg/bzz" "github.com/ethersphere/bee/v2/pkg/crypto" "github.com/ethersphere/bee/v2/pkg/statestore/mock" + "github.com/ethersphere/bee/v2/pkg/storage" "github.com/ethersphere/bee/v2/pkg/swarm" ma "github.com/multiformats/go-multiaddr" ) @@ -96,3 +98,44 @@ func run(t *testing.T, f bookFunc) { t.Fatalf("expected addresses len %v, got %v", 1, len(addresses)) } } + +type mockCorruptedStore struct{} + +func (m *mockCorruptedStore) Get(key string, i any) error { + corruptedJSON := []byte(`{"address": null}`) + return json.Unmarshal(corruptedJSON, i) +} + +func (m *mockCorruptedStore) Put(key string, i any) error { + return nil +} + +func (m *mockCorruptedStore) Delete(key string) error { + return nil +} + +func (m *mockCorruptedStore) Iterate(prefix string, fn storage.StateIterFunc) error { + return nil +} + +func (m *mockCorruptedStore) Close() error { + return nil +} + +func TestGetCorruptedNilAddress(t *testing.T) { + t.Parallel() + + corruptedStore := &mockCorruptedStore{} + book := addressbook.New(corruptedStore) + + addr := swarm.NewAddress([]byte{0, 1, 2, 3}) + + v, _, err := book.Get(addr) + if !errors.Is(err, addressbook.ErrNotFound) { + t.Fatalf("expected ErrNotFound for corrupted entry, got %v", err) + } + + if v != nil { + t.Fatalf("expected nil address, got %s", v) + } +}