Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issues discovered through fuzz testing #2659

Merged
merged 2 commits into from
Aug 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 0 additions & 4 deletions ast/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -669,10 +669,6 @@ func (rc *refChecker) checkRefLeaf(tpe types.Type, ref Ref, idx int) *Error {
}

case *Array, Object, Set:
// Composite references operands may only be used with a set.
if !unifies(tpe, types.NewSet(types.A)) {
return newRefErrInvalid(ref[0].Location, rc.varRewriter(ref), idx, tpe, types.NewSet(types.A), nil)
}
if !unify1(rc.env, head, keys, false) {
return newRefErrInvalid(ref[0].Location, rc.varRewriter(ref), idx, rc.env.Get(head), keys, nil)
}
Expand Down
24 changes: 21 additions & 3 deletions ast/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ func TestCheckInference(t *testing.T) {
nil,
),
}},
{"object-composite-ref-operand", `x = {{}: 1}; x[{}] = y`, map[Var]types.Type{
Var("x"): types.NewObject(
[]*types.StaticProperty{types.NewStaticProperty(
map[string]interface{}{},
types.N,
)},
nil,
),
Var("y"): types.N,
}},
{"sets", `x = {1, 2}; y = {{"foo", 1}, x}`, map[Var]types.Type{
Var("x"): types.NewSet(types.N),
Var("y"): types.NewSet(
Expand Down Expand Up @@ -795,12 +805,12 @@ func TestCheckRefErrInvalid(t *testing.T) {
oneOf: []Value{String("p"), String("q")},
},
{
note: "composite ref into non-set",
note: "composite ref operand",
query: `data.test.q[[1, 2]]`,
ref: "data.test.q[[1, 2]]",
pos: 3,
have: types.NewObject([]*types.StaticProperty{types.NewStaticProperty("bar", types.N), types.NewStaticProperty("foo", types.N)}, nil),
want: types.NewSet(types.A),
have: types.NewArray([]types.Type{types.N, types.N}, nil),
want: types.S,
},
{
note: "composite ref type error 1",
Expand All @@ -818,6 +828,14 @@ func TestCheckRefErrInvalid(t *testing.T) {
have: types.NewObject([]*types.StaticProperty{types.NewStaticProperty("a", types.S)}, nil),
want: types.NewObject([]*types.StaticProperty{types.NewStaticProperty("a", types.N)}, nil),
},
{
note: "composite ref type error 3 - array",
query: `a = [1,2,3]; a[{}] = b`,
ref: `a[{}]`,
pos: 1,
have: types.NewObject(nil, types.NewDynamicProperty(types.A, types.A)),
want: types.N,
},
}

for _, tc := range tests {
Expand Down
7 changes: 7 additions & 0 deletions ast/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -3033,6 +3033,13 @@ func rewriteDynamicsComprehensionBody(original *Expr, f *equalityFactory, body B
}

func rewriteExprTermsInHead(gen *localVarGenerator, rule *Rule) {
for i := range rule.Head.Args {
support, output := expandExprTerm(gen, rule.Head.Args[i])
for j := range support {
rule.Body.Append(support[j])
}
rule.Head.Args[i] = output
}
if rule.Head.Key != nil {
support, output := expandExprTerm(gen, rule.Head.Key)
for i := range support {
Expand Down
11 changes: 11 additions & 0 deletions ast/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1197,6 +1197,17 @@ func TestCompilerRewriteExprTerms(t *testing.T) {
`,
expected: Errors{&Error{Message: "rule arguments cannot contain calls"}},
},
{
note: "indirect ref in args",
module: `
package test

f([1][0]) { true }`,
expected: `
package test

f(__local0__[0]) { true; __local0__ = [1] }`,
},
}

for _, tc := range cases {
Expand Down
9 changes: 9 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -822,6 +822,15 @@ func TypeOf(x interface{}) Type {
return S
case json.Number:
return N
case map[string]interface{}:
// The ast.ValueToInterface() function returns ast.Object values as map[string]interface{}
// so map[string]interface{} must be handled here because the type checker uses the value
// to interface conversion when inferring object types.
static := make([]*StaticProperty, 0, len(x))
for k, v := range x {
static = append(static, NewStaticProperty(k, TypeOf(v)))
}
return NewObject(static, nil)
case map[interface{}]interface{}:
static := make([]*StaticProperty, 0, len(x))
for k, v := range x {
Expand Down
16 changes: 16 additions & 0 deletions types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,22 @@ func TestTypeOf(t *testing.T) {
}
}

func TestTypeOfMapOfString(t *testing.T) {
tpe := TypeOf(map[string]interface{}{
"foo": "bar",
"baz": "qux",
})

exp := NewObject([]*StaticProperty{
NewStaticProperty("foo", S),
NewStaticProperty("baz", S),
}, nil)

if Compare(exp, tpe) != 0 {
t.Fatalf("Expected %v but got: %v", exp, tpe)
}
}

func TestNil(t *testing.T) {

tpe := NewObject([]*StaticProperty{
Expand Down