Skip to content

Commit

Permalink
[FAB-5525] Fix configtx memory allocation bug
Browse files Browse the repository at this point in the history
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 <jyellick@us.ibm.com>
  • Loading branch information
Jason Yellick committed Jul 28, 2017
1 parent 3e4ae31 commit b3c1430
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion common/configtx/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
15 changes: 15 additions & 0 deletions common/configtx/configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit b3c1430

Please sign in to comment.