From b3c14300f6c9162c01216e54e4243705bddce225 Mon Sep 17 00:00:00 2001 From: Jason Yellick Date: Fri, 28 Jul 2017 15:28:53 -0400 Subject: [PATCH] [FAB-5525] Fix configtx memory allocation bug The configtx code uses an 'append(...)' against a slice, and records the new slice address when computing the config map. Through sheer dumb luck, this code works for config groups which are only nested 2 levels deep, because the append call triggers a true new memory allocation. However, for config groups 3 levels deep (such as consortium groups), the append call actually re-uses the underlying memory for the slice. This causes the path to be corrupted internally for the consortium group items, and cause the wrong policy to be resolved when checking for a policy which has been specified relatively. This fix manually allocates new memory, copies it, and then appends the element. Change-Id: I0f4df619e006cdfebba60173156bda597d42a544 Signed-off-by: Jason Yellick --- common/configtx/configmap.go | 4 +++- common/configtx/configmap_test.go | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/common/configtx/configmap.go b/common/configtx/configmap.go index 113a80a494b..6631e8f8ba8 100644 --- a/common/configtx/configmap.go +++ b/common/configtx/configmap.go @@ -83,7 +83,9 @@ func recurseConfig(result map[string]comparable, path []string, group *cb.Config } for key, group := range group.Groups { - nextPath := append(path, key) + nextPath := make([]string, len(path)+1) + copy(nextPath, path) + nextPath[len(nextPath)-1] = key if err := recurseConfig(result, nextPath, group); err != nil { return err } diff --git a/common/configtx/configmap_test.go b/common/configtx/configmap_test.go index 776360290da..f38810df9f3 100644 --- a/common/configtx/configmap_test.go +++ b/common/configtx/configmap_test.go @@ -29,6 +29,21 @@ func TestBadKey(t *testing.T) { "Should have errored on key with illegal characters") } +func TestConfigMapMultiGroup(t *testing.T) { + config := cb.NewConfigGroup() + config.Groups["0"] = cb.NewConfigGroup() + config.Groups["0"].Groups["1"] = cb.NewConfigGroup() + config.Groups["0"].Groups["1"].Groups["2.1"] = cb.NewConfigGroup() + config.Groups["0"].Groups["1"].Groups["2.1"].Values["Value"] = &cb.ConfigValue{} + config.Groups["0"].Groups["1"].Groups["2.2"] = cb.NewConfigGroup() + config.Groups["0"].Groups["1"].Groups["2.2"].Values["Value"] = &cb.ConfigValue{} + + confMap, err := MapConfig(config) + assert.NoError(t, err) + assert.Equal(t, []string{"Channel", "0", "1", "2.1"}, confMap["[Values] /Channel/0/1/2.1/Value"].path) + assert.Equal(t, []string{"Channel", "0", "1", "2.2"}, confMap["[Values] /Channel/0/1/2.2/Value"].path) +} + func TestConfigMap(t *testing.T) { config := cb.NewConfigGroup() config.Groups["0DeepGroup"] = cb.NewConfigGroup()