mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
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:
+7
-5
@@ -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
|
||||
}
|
||||
|
||||
@@ -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},
|
||||
|
||||
Reference in New Issue
Block a user