Skip to content

Commit 00c255f

Browse files
committed
Fix NewUDPMuxDefault panic
1 parent 398ac7c commit 00c255f

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

error_conn.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// SPDX-FileCopyrightText: 2026 The Pion community <https://pion.ly>
2+
// SPDX-License-Identifier: MIT
3+
4+
package ice
5+
6+
import (
7+
"net"
8+
"time"
9+
)
10+
11+
// ErrorConn for net.PacketConn when the value is nil.
12+
type ErrorConn struct{}
13+
14+
func (e *ErrorConn) ReadFrom([]byte) (n int, addr net.Addr, err error) {
15+
var dummyAddr *net.IPAddr
16+
17+
return 0, dummyAddr, ErrPacketConnNil
18+
} //nolint:revive
19+
func (e *ErrorConn) WriteTo([]byte, net.Addr) (n int, err error) { return 0, ErrPacketConnNil } //nolint:revive
20+
func (e *ErrorConn) Close() error { return ErrPacketConnNil } //nolint:revive
21+
func (e *ErrorConn) LocalAddr() net.Addr {
22+
var dummyAddr *net.IPAddr
23+
24+
return dummyAddr
25+
} //nolint:revive
26+
func (e *ErrorConn) SetDeadline(time.Time) error { return ErrPacketConnNil } //nolint:revive
27+
func (e *ErrorConn) SetReadDeadline(time.Time) error { return ErrPacketConnNil } //nolint:revive
28+
func (e *ErrorConn) SetWriteDeadline(time.Time) error { return ErrPacketConnNil } //nolint:revive

errors.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ var (
172172
// ErrAgentOptionNotUpdatable indicates an option cannot be updated after construction.
173173
ErrAgentOptionNotUpdatable = errors.New("option can only be set during agent construction")
174174

175+
// ErrAgentOptionNotUpdatable indicates an net.PacketConn value was nil.
176+
ErrPacketConnNil = errors.New("net.PacketConn is nil")
177+
175178
errAttributeTooShortICECandidate = errors.New("attribute not long enough to be ICE candidate")
176179
errClosingConnection = errors.New("failed to close connection")
177180
errConnectionAddrAlreadyExist = errors.New("connection with same remote address already exists")

udp_mux.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ func NewUDPMuxDefault(params UDPMuxParams) *UDPMuxDefault { //nolint:cyclop
6767
}
6868

6969
var localAddrsForUnspecified []net.Addr
70-
if udpAddr, ok := params.UDPConn.LocalAddr().(*net.UDPAddr); !ok { //nolint:nestif
70+
//nolint:nestif
71+
if params.UDPConn == nil {
72+
params.Logger.Errorf("UDPConn is nil")
73+
params.UDPConn = &ErrorConn{}
74+
} else if udpAddr, ok := params.UDPConn.LocalAddr().(*net.UDPAddr); !ok { //nolint:nestif
7175
params.Logger.Errorf("LocalAddr is not a net.UDPAddr, got %T", params.UDPConn.LocalAddr())
7276
} else if ok && udpAddr.IP.IsUnspecified() {
7377
// For unspecified addresses, the correct behavior is to return errListenUnspecified, but

0 commit comments

Comments
 (0)