Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
internal/core/adt: prevent nil interface bug for Source
Browse files Browse the repository at this point in the history
Fixes #521

Change-Id: I59c789f72ba9c6597378279f26458d77721a4da1
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7122
Reviewed-by: Paul Jolly <paul@myitcv.org.uk>
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
  • Loading branch information
mpvl committed Sep 16, 2020
1 parent bd3dd75 commit db18b37
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
12 changes: 10 additions & 2 deletions internal/core/adt/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,13 @@ func (x *StructMarker) node() {}
// Top represents all possible values. It can be used as a Value and Expr.
type Top struct{ Src *ast.Ident }

func (x *Top) Source() ast.Node { return x.Src }
func (x *Top) Kind() Kind { return TopKind }
func (x *Top) Source() ast.Node {
if x.Src == nil {
return nil
}
return x.Src
}
func (x *Top) Kind() Kind { return TopKind }

// BasicType represents all values of a certain Kind. It can be used as a Value
// and Expr.
Expand Down Expand Up @@ -1246,6 +1251,9 @@ type ValueClause struct {
}

func (x *ValueClause) Source() ast.Node {
if x.StructLit == nil {
return nil
}
if x.Src == nil {
return nil
}
Expand Down
74 changes: 74 additions & 0 deletions internal/core/adt/expr_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright 2020 CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package adt

import (
"fmt"
"testing"
)

func TestNilSource(t *testing.T) {
testCases := []Node{
&BasicType{},
&BinaryExpr{},
&Bool{},
&Bottom{},
&BoundExpr{},
&BoundValue{},
&Builtin{},
&BuiltinValidator{},
&BulkOptionalField{},
&Bytes{},
&CallExpr{},
&Conjunction{},
&Disjunction{},
&DisjunctionExpr{},
&DynamicField{},
&DynamicReference{},
&Ellipsis{},
&Field{},
&FieldReference{},
&ForClause{},
&IfClause{},
&ImportReference{},
&IndexExpr{},
&Interpolation{},
&LabelReference{},
&LetClause{},
&LetReference{},
&ListLit{},
&ListMarker{},
&NodeLink{},
&Null{},
&Num{},
&OptionalField{},
&SelectorExpr{},
&SliceExpr{},
&String{},
&StructLit{},
&StructMarker{},
&Top{},
&UnaryExpr{},
&ValueClause{},
&Vertex{},
}
for _, x := range testCases {
t.Run(fmt.Sprintf("%T", x), func(t *testing.T) {
if x.Source() != nil {
t.Error("nil source did not return nil")
}
})
}
}

0 comments on commit db18b37

Please sign in to comment.