core: check for negative indices in ResourceConfig.get

The bounds checking in ResourceConfig.get() was insufficient: it detected when the index was greater than or equal to cv.Len() but not when the index was less than zero. If the user provided an (invalid) configuration that referenced "foo.-1.bar", the provider would panic.

Now it behaves the same way as if the index were too high.
This commit is contained in:
Florian Forster 2017-12-12 18:18:38 +01:00 committed by Martin Atkins
parent f1079257ac
commit 6680b1f16b
2 changed files with 9 additions and 1 deletions

View File

@ -346,7 +346,7 @@ func (c *ResourceConfig) get(
if err != nil {
return nil, false
}
if i >= int64(cv.Len()) {
if int(i) < 0 || int(i) >= cv.Len() {
return nil, false
}
current = cv.Index(int(i)).Interface()

View File

@ -158,6 +158,14 @@ func TestResourceConfigGet(t *testing.T) {
Value: nil,
},
{
Config: map[string]interface{}{
"foo": []interface{}{1, 2, 5},
},
Key: "foo.-1",
Value: nil,
},
// get from map
{
Config: map[string]interface{}{