[#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 <leonard@nspcc.ru>
This commit is contained in:
parent
72d81dba92
commit
e26dc0a6e3
4 changed files with 46 additions and 2 deletions
|
@ -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),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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"))
|
||||
})
|
||||
}
|
||||
|
|
|
@ -1,7 +1,18 @@
|
|||
{
|
||||
"value": "some value",
|
||||
|
||||
"section": {
|
||||
"any": "thing"
|
||||
"any": "thing",
|
||||
"sub": {
|
||||
"sub": {
|
||||
"sub1": {
|
||||
"key": "val1"
|
||||
},
|
||||
"sub2": {
|
||||
"key": "val2"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
"string_slice": {
|
||||
|
|
|
@ -3,6 +3,14 @@ value: some value
|
|||
section:
|
||||
any: thing
|
||||
|
||||
sub:
|
||||
sub:
|
||||
sub1:
|
||||
key: val1
|
||||
|
||||
sub2:
|
||||
key: val2
|
||||
|
||||
string_slice:
|
||||
empty: []
|
||||
|
||||
|
|
Loading…
Reference in a new issue