Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

fix: avoid returning typed nils #257

Merged
merged 1 commit into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion swarm_dial.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,12 @@ func (s *Swarm) DialPeer(ctx context.Context, p peer.ID) (network.Conn, error) {
return nil, &DialError{Peer: p, Cause: ErrGaterDisallowedConnection}
}

return s.dialPeer(ctx, p)
// Avoid typed nil issues.
c, err := s.dialPeer(ctx, p)
if err != nil {
return nil, err
}
return c, nil
}

// internal dial method that returns an unwrapped conn
Expand Down
13 changes: 13 additions & 0 deletions swarm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,19 @@ func TestCloseWithOpenStreams(t *testing.T) {
}
}

func TestTypedNilConn(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
s := GenSwarm(t, ctx)
defer s.Close()

// We can't dial ourselves.
c, err := s.DialPeer(ctx, s.LocalPeer())
require.Error(t, err)
// If we fail to dial, the connection should be nil.
require.True(t, c == nil)
}

func TestPreventDialListenAddr(t *testing.T) {
s := GenSwarm(t, context.Background(), OptDialOnly)
if err := s.Listen(ma.StringCast("/ip4/0.0.0.0/udp/0/quic")); err != nil {
Expand Down