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 GitHub
parent e00a280329
commit ca9aca9b19
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 38 additions and 7 deletions

View File

@ -13,7 +13,6 @@ import (
"sort" "sort"
"strings" "strings"
"sync" "sync"
"sync/atomic"
"time" "time"
"github.com/hashicorp/go-multierror" "github.com/hashicorp/go-multierror"
@ -146,7 +145,7 @@ type Engine struct {
STUNs []*stun.URI STUNs []*stun.URI
// TURNs is a list of STUN servers used by ICE // TURNs is a list of STUN servers used by ICE
TURNs []*stun.URI TURNs []*stun.URI
stunTurn atomic.Value stunTurn icemaker.StunTurn
clientCtx context.Context clientCtx context.Context
clientCancel context.CancelFunc 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/ice/v3"
"github.com/pion/randutil" "github.com/pion/randutil"
"github.com/pion/stun/v2"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/netbirdio/netbird/client/internal/stdnet" "github.com/netbirdio/netbird/client/internal/stdnet"
@ -39,7 +38,7 @@ func NewAgent(iFaceDiscover stdnet.ExternalIFaceDiscover, config Config, candida
agentConfig := &ice.AgentConfig{ agentConfig := &ice.AgentConfig{
MulticastDNSMode: ice.MulticastDNSModeDisabled, MulticastDNSMode: ice.MulticastDNSModeDisabled,
NetworkTypes: []ice.NetworkType{ice.NetworkTypeUDP4, ice.NetworkTypeUDP6}, NetworkTypes: []ice.NetworkType{ice.NetworkTypeUDP4, ice.NetworkTypeUDP6},
Urls: config.StunTurn.Load().([]*stun.URI), Urls: config.StunTurn.Load(),
CandidateTypes: candidateTypes, CandidateTypes: candidateTypes,
InterfaceFilter: stdnet.InterfaceFilter(config.InterfaceBlackList), InterfaceFilter: stdnet.InterfaceFilter(config.InterfaceBlackList),
UDPMux: config.UDPMux, UDPMux: config.UDPMux,

View File

@ -1,14 +1,12 @@
package ice package ice
import ( import (
"sync/atomic"
"github.com/pion/ice/v3" "github.com/pion/ice/v3"
) )
type Config struct { type Config struct {
// StunTurn is a list of STUN and TURN URLs // 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 // 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) // (e.g. if eth0 is in the list, host candidate of this interface won't be used)