Skip to content

Commit

Permalink
add stronger protections for nil pointers in server join merge
Browse files Browse the repository at this point in the history
  • Loading branch information
chelseakomlo committed May 29, 2018
1 parent 1707b3c commit b02a881
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 21 deletions.
24 changes: 18 additions & 6 deletions command/agent/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -381,19 +381,31 @@ type ServerJoin struct {
RetryInterval time.Duration `mapstructure:"retry_interval"`
}

func (s *ServerJoin) Merge(b *ServerJoin) {
func (s *ServerJoin) Merge(b *ServerJoin) *ServerJoin {
if s == nil {
return b
}

result := *s

if b == nil {
return &result
}

if len(b.StartJoin) != 0 {
s.StartJoin = b.StartJoin
result.StartJoin = b.StartJoin
}
if len(b.RetryJoin) != 0 {
s.RetryJoin = b.RetryJoin
result.RetryJoin = b.RetryJoin
}
if b.RetryMaxAttempts != 0 {
s.RetryMaxAttempts = b.RetryMaxAttempts
result.RetryMaxAttempts = b.RetryMaxAttempts
}
if b.RetryInterval != 0 {
s.RetryInterval = b.RetryInterval
result.RetryInterval = b.RetryInterval
}

return &result
}

// EncryptBytes returns the encryption key configured.
Expand Down Expand Up @@ -1104,7 +1116,7 @@ func (a *ServerConfig) Merge(b *ServerConfig) *ServerConfig {
result.EncryptKey = b.EncryptKey
}
if b.ServerJoin != nil {
result.ServerJoin.Merge(b.ServerJoin)
result.ServerJoin = result.ServerJoin.Merge(b.ServerJoin)
}

// Add the schedulers
Expand Down
70 changes: 55 additions & 15 deletions command/agent/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -926,11 +926,11 @@ func TestMergeServerJoin(t *testing.T) {
}
b := &ServerJoin{}

a.Merge(b)
require.Equal(a.RetryJoin, retryJoin)
require.Equal(a.StartJoin, startJoin)
require.Equal(a.RetryMaxAttempts, retryMaxAttempts)
require.Equal(a.RetryInterval, retryInterval)
result := a.Merge(b)
require.Equal(result.RetryJoin, retryJoin)
require.Equal(result.StartJoin, startJoin)
require.Equal(result.RetryMaxAttempts, retryMaxAttempts)
require.Equal(result.RetryInterval, retryInterval)
}
{
retryJoin := []string{"127.0.0.1", "127.0.0.2"}
Expand All @@ -946,11 +946,51 @@ func TestMergeServerJoin(t *testing.T) {
RetryInterval: time.Duration(retryInterval),
}

a.Merge(b)
require.Equal(a.RetryJoin, retryJoin)
require.Equal(a.StartJoin, startJoin)
require.Equal(a.RetryMaxAttempts, retryMaxAttempts)
require.Equal(a.RetryInterval, retryInterval)
result := a.Merge(b)
require.Equal(result.RetryJoin, retryJoin)
require.Equal(result.StartJoin, startJoin)
require.Equal(result.RetryMaxAttempts, retryMaxAttempts)
require.Equal(result.RetryInterval, retryInterval)
}
{
retryJoin := []string{"127.0.0.1", "127.0.0.2"}
startJoin := []string{"127.0.0.1", "127.0.0.2"}
retryMaxAttempts := 1
retryInterval := time.Duration(0)

var a *ServerJoin
b := &ServerJoin{
RetryJoin: retryJoin,
StartJoin: startJoin,
RetryMaxAttempts: retryMaxAttempts,
RetryInterval: time.Duration(retryInterval),
}

result := a.Merge(b)
require.Equal(result.RetryJoin, retryJoin)
require.Equal(result.StartJoin, startJoin)
require.Equal(result.RetryMaxAttempts, retryMaxAttempts)
require.Equal(result.RetryInterval, retryInterval)
}
{
retryJoin := []string{"127.0.0.1", "127.0.0.2"}
startJoin := []string{"127.0.0.1", "127.0.0.2"}
retryMaxAttempts := 1
retryInterval := time.Duration(0)

a := &ServerJoin{
RetryJoin: retryJoin,
StartJoin: startJoin,
RetryMaxAttempts: retryMaxAttempts,
RetryInterval: time.Duration(retryInterval),
}
var b *ServerJoin

result := a.Merge(b)
require.Equal(result.RetryJoin, retryJoin)
require.Equal(result.StartJoin, startJoin)
require.Equal(result.RetryMaxAttempts, retryMaxAttempts)
require.Equal(result.RetryInterval, retryInterval)
}
{
retryJoin := []string{"127.0.0.1", "127.0.0.2"}
Expand All @@ -967,10 +1007,10 @@ func TestMergeServerJoin(t *testing.T) {
RetryInterval: time.Duration(retryInterval),
}

a.Merge(b)
require.Equal(a.RetryJoin, retryJoin)
require.Equal(a.StartJoin, startJoin)
require.Equal(a.RetryMaxAttempts, retryMaxAttempts)
require.Equal(a.RetryInterval, retryInterval)
result := a.Merge(b)
require.Equal(result.RetryJoin, retryJoin)
require.Equal(result.StartJoin, startJoin)
require.Equal(result.RetryMaxAttempts, retryMaxAttempts)
require.Equal(result.RetryInterval, retryInterval)
}
}

0 comments on commit b02a881

Please sign in to comment.