From 1a6c67d106571853c77c08ddf19859da76ac06d4 Mon Sep 17 00:00:00 2001 From: David Bresson Date: Fri, 3 Feb 2017 08:05:35 -0800 Subject: [PATCH 1/2] extend config merge test to test merging to empty config --- command/agent/config_test.go | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/command/agent/config_test.go b/command/agent/config_test.go index 35d90f8b568..9cce09e24f9 100644 --- a/command/agent/config_test.go +++ b/command/agent/config_test.go @@ -20,7 +20,21 @@ var ( ) func TestConfig_Merge(t *testing.T) { + c0 := &Config{} + c1 := &Config{ + Telemetry: &Telemetry{}, + Client: &ClientConfig{}, + Server: &ServerConfig{}, + Ports: &Ports{}, + Addresses: &Addresses{}, + AdvertiseAddrs: &AdvertiseAddrs{}, + Atlas: &AtlasConfig{}, + Vault: &config.VaultConfig{}, + Consul: &config.ConsulConfig{}, + } + + c2 := &Config{ Region: "global", Datacenter: "dc1", NodeName: "node1", @@ -136,7 +150,7 @@ func TestConfig_Merge(t *testing.T) { }, } - c2 := &Config{ + c3 := &Config{ Region: "region2", Datacenter: "dc2", NodeName: "node2", @@ -271,9 +285,11 @@ func TestConfig_Merge(t *testing.T) { }, } - result := c1.Merge(c2) - if !reflect.DeepEqual(result, c2) { - t.Fatalf("bad:\n%#v\n%#v", result, c2) + result := c0.Merge(c1) + result = result.Merge(c2) + result = result.Merge(c3) + if !reflect.DeepEqual(result, c3) { + t.Fatalf("bad:\n%#v\n%#v", result, c3) } } From a49759f1b877ac43d8067f6f94af36584eb2492c Mon Sep 17 00:00:00 2001 From: David Bresson Date: Fri, 3 Feb 2017 08:39:57 -0800 Subject: [PATCH 2/2] avoid nil pointer dereference --- command/agent/config.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/command/agent/config.go b/command/agent/config.go index f842662f196..dc90c740fdc 100644 --- a/command/agent/config.go +++ b/command/agent/config.go @@ -898,7 +898,10 @@ func (a *ClientConfig) Merge(b *ClientConfig) *ClientConfig { if b.ClientMinPort != 0 { result.ClientMinPort = b.ClientMinPort } - if b.Reserved != nil { + if result.Reserved == nil && b.Reserved != nil { + reserved := *b.Reserved + result.Reserved = &reserved + } else if b.Reserved != nil { result.Reserved = result.Reserved.Merge(b.Reserved) } if b.GCInterval != 0 {