Fix nil pointer exception when load empty list and try to cast it (#3282)

This commit is contained in:
Zoltan Papp 2025-02-06 10:20:31 +01:00 committed by Viktor Liu
parent 206420c085
commit eb69f2de78
5 changed files with 38 additions and 7 deletions

View File

@ -13,7 +13,6 @@ import (
"sort"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/hashicorp/go-multierror"
@ -147,7 +146,7 @@ type Engine struct {
STUNs []*stun.URI
// TURNs is a list of STUN servers used by ICE
TURNs []*stun.URI
stunTurn atomic.Value
stunTurn icemaker.StunTurn
clientCtx context.Context
clientCancel context.CancelFunc

View File

@ -0,0 +1,22 @@
package ice
import (
"sync/atomic"
"github.com/pion/stun/v2"
)
type StunTurn atomic.Value
func (s *StunTurn) Load() []*stun.URI {
v := (*atomic.Value)(s).Load()
if v == nil {
return nil
}
return v.([]*stun.URI)
}
func (s *StunTurn) Store(value []*stun.URI) {
(*atomic.Value)(s).Store(value)
}

View File

@ -0,0 +1,13 @@
package ice
import (
"testing"
)
func TestStunTurn_LoadEmpty(t *testing.T) {
var stStunTurn StunTurn
got := stStunTurn.Load()
if len(got) != 0 {
t.Errorf("StunTurn.Load() = %v, want %v", got, nil)
}
}

View File

@ -5,7 +5,6 @@ import (
"github.com/pion/ice/v3"
"github.com/pion/randutil"
"github.com/pion/stun/v2"
log "github.com/sirupsen/logrus"
"github.com/netbirdio/netbird/client/internal/stdnet"
@ -39,7 +38,7 @@ func NewAgent(iFaceDiscover stdnet.ExternalIFaceDiscover, config Config, candida
agentConfig := &ice.AgentConfig{
MulticastDNSMode: ice.MulticastDNSModeDisabled,
NetworkTypes: []ice.NetworkType{ice.NetworkTypeUDP4, ice.NetworkTypeUDP6},
Urls: config.StunTurn.Load().([]*stun.URI),
Urls: config.StunTurn.Load(),
CandidateTypes: candidateTypes,
InterfaceFilter: stdnet.InterfaceFilter(config.InterfaceBlackList),
UDPMux: config.UDPMux,

View File

@ -1,14 +1,12 @@
package ice
import (
"sync/atomic"
"github.com/pion/ice/v3"
)
type Config struct {
// StunTurn is a list of STUN and TURN URLs
StunTurn *atomic.Value // []*stun.URI
StunTurn *StunTurn // []*stun.URI
// InterfaceBlackList is a list of machine interfaces that should be filtered out by ICE Candidate gathering
// (e.g. if eth0 is in the list, host candidate of this interface won't be used)