From e26dc0a6e3c89b2ef4ea7eeaaaedd591ac44da14 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 1 Jun 2021 20:12:00 +0300 Subject: [PATCH] [#493] node/config: Fix corrupting of path to the subsection In previous implementation `Config.Sub` method could lead to the violation of the internal `path` slice because of `append`. This has been observed on deeply nested subsections. Fix `Config.Sub` to copy internal slice in order to prevent violations. Cover problem case in test config files and unit test. Signed-off-by: Leonard Lyubich --- cmd/neofs-node/config/calls.go | 9 ++++++++- cmd/neofs-node/config/calls_test.go | 18 ++++++++++++++++++ cmd/neofs-node/config/test/config.json | 13 ++++++++++++- cmd/neofs-node/config/test/config.yaml | 8 ++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/cmd/neofs-node/config/calls.go b/cmd/neofs-node/config/calls.go index 56ffd4371..4f15351df 100644 --- a/cmd/neofs-node/config/calls.go +++ b/cmd/neofs-node/config/calls.go @@ -8,9 +8,16 @@ import ( // // Returns nil if subsection if missing. func (x *Config) Sub(name string) *Config { + // copy path in order to prevent consequent violations + ln := len(x.path) + + path := make([]string, ln, ln+1) + + copy(path, x.path) + return &Config{ v: x.v, - path: append(x.path, name), + path: append(path, name), } } diff --git a/cmd/neofs-node/config/calls_test.go b/cmd/neofs-node/config/calls_test.go index 6408447c2..52425ab70 100644 --- a/cmd/neofs-node/config/calls_test.go +++ b/cmd/neofs-node/config/calls_test.go @@ -42,3 +42,21 @@ func TestConfigEnv(t *testing.T) { require.Equal(t, value, c.Sub(section).Value(name)) } + +func TestConfig_SubValue(t *testing.T) { + configtest.ForEachFileType("test/config", func(c *config.Config) { + c = c. + Sub("section"). + Sub("sub"). + Sub("sub") + + // get subsection 1 + sub := c.Sub("sub1") + + // get subsection 2 + c.Sub("sub2") + + // sub should not be corrupted + require.Equal(t, "val1", sub.Value("key")) + }) +} diff --git a/cmd/neofs-node/config/test/config.json b/cmd/neofs-node/config/test/config.json index 8332e3e84..e2d00fcab 100644 --- a/cmd/neofs-node/config/test/config.json +++ b/cmd/neofs-node/config/test/config.json @@ -1,7 +1,18 @@ { "value": "some value", + "section": { - "any": "thing" + "any": "thing", + "sub": { + "sub": { + "sub1": { + "key": "val1" + }, + "sub2": { + "key": "val2" + } + } + } }, "string_slice": { diff --git a/cmd/neofs-node/config/test/config.yaml b/cmd/neofs-node/config/test/config.yaml index 1b5f6c07d..6ee4aca0f 100644 --- a/cmd/neofs-node/config/test/config.yaml +++ b/cmd/neofs-node/config/test/config.yaml @@ -3,6 +3,14 @@ value: some value section: any: thing + sub: + sub: + sub1: + key: val1 + + sub2: + key: val2 + string_slice: empty: []