Skip to content

Commit

Permalink
Merge pull request #257 from libp2p/fix/typed-nil
Browse files Browse the repository at this point in the history
fix: avoid returning typed nils
  • Loading branch information
Stebalien authored Apr 22, 2021
2 parents 19c7253 + 98d7eef commit 4e6eff8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
7 changes: 6 additions & 1 deletion p2p/net/swarm/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 p2p/net/swarm/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

0 comments on commit 4e6eff8

Please sign in to comment.