types: Fix constant select on array types

If a negative array index was hardcoded in the policy it would cause a
panic (e.g., arr[-1]). This patch just fixes the select function to
return nil like it does for out-of-bounds.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
This commit is contained in:
Torin Sandall
2019-11-07 16:41:18 -05:00
parent 297e1f0525
commit 4d8bd0f516
2 changed files with 9 additions and 5 deletions
+7 -5
View File
@@ -182,11 +182,13 @@ func (t *Array) Len() int {
// Select returns the type of element at the zero-based pos.
func (t *Array) Select(pos int) Type {
if len(t.static) > pos {
return t.static[pos]
}
if t.dynamic != nil {
return t.dynamic
if pos >= 0 {
if len(t.static) > pos {
return t.static[pos]
}
if t.dynamic != nil {
return t.dynamic
}
}
return nil
}
+2
View File
@@ -192,6 +192,8 @@ func TestSelect(t *testing.T) {
{"static", NewArray([]Type{S}, nil), json.Number("0"), S},
{"dynamic", NewArray(nil, S), json.Number("100"), S},
{"out of range", NewArray([]Type{S, N, B}, nil), json.Number("4"), nil},
{"out of range negative", NewArray([]Type{S, N, B}, nil), json.Number("-4"), nil},
{"negative", NewArray([]Type{S, N, B}, nil), json.Number("-2"), nil},
{"non int", NewArray([]Type{S, N, B}, nil), json.Number("1.5"), nil},
{"non int-2", NewArray([]Type{S, N, B}, nil), 1, nil},
{"static", NewObject([]*StaticProperty{NewStaticProperty("hello", S)}, nil), "hello", S},