Skip to content

Commit

Permalink
Fix panic when getting XORMapped addr (netbirdio#874)
Browse files Browse the repository at this point in the history
  • Loading branch information
braginini authored May 18, 2023
1 parent 6e9f753 commit 3876cb2
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions iface/bind/udp_mux_universal.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,19 @@ func (m *UniversalUDPMuxDefault) GetXORMappedAddr(serverAddr net.Addr, deadline
select {
case <-waitAddrReceived:
// when channel closed, addr was obtained
var addr *stun.XORMappedAddress
m.mu.Lock()
mappedAddr := *m.xorMappedMap[serverAddr.String()]
// A very odd case that mappedAddr is nil.
// But can happen when the deadline property is larger than params.XORMappedAddrCacheTTL.
// We protect the code from panic.
if mappedAddr, ok := m.xorMappedMap[serverAddr.String()]; ok {
addr = mappedAddr.addr
}
m.mu.Unlock()
if mappedAddr.addr == nil {
if addr == nil {
return nil, fmt.Errorf("no XOR address mapping")
}
return mappedAddr.addr, nil
return addr, nil
case <-time.After(deadline):
return nil, fmt.Errorf("timeout while waiting for XORMappedAddr")
}
Expand Down

0 comments on commit 3876cb2

Please sign in to comment.