Fix panic when getting XORMapped addr (#874)

This commit is contained in:
Misha Bragin 2023-05-18 18:50:46 +02:00 committed by GitHub
parent 6e9f7531f5
commit 3876cb26f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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")
}