Merge pull request #280 from asmundak/master

Fix null pointer dereference printing an expression.
This commit is contained in:
asmundak 2020-01-27 10:17:26 -08:00 committed by GitHub
commit 30f225ba8a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 0 deletions

View file

@ -164,6 +164,7 @@ const (
Int64Type
ListType
MapType
NotEvaluatedType
)
func (t Type) String() string {
@ -178,6 +179,8 @@ func (t Type) String() string {
return "list"
case MapType:
return "map"
case NotEvaluatedType:
return "notevaluated"
default:
panic(fmt.Errorf("Unknown type %d", t))
}
@ -476,6 +479,29 @@ func (c Comment) Text() string {
return string(buf)
}
type NotEvaluated struct {
Position scanner.Position
}
func (n NotEvaluated) Copy() Expression {
return NotEvaluated{Position: n.Position}
}
func (n NotEvaluated) String() string {
return "Not Evaluated"
}
func (n NotEvaluated) Type() Type {
return NotEvaluatedType
}
func (n NotEvaluated) Eval() Expression {
return NotEvaluated{Position: n.Position}
}
func (n NotEvaluated) Pos() scanner.Position { return n.Position }
func (n NotEvaluated) End() scanner.Position { return n.Position }
func endPos(pos scanner.Position, n int) scanner.Position {
pos.Offset += n
pos.Column += n

View file

@ -484,6 +484,8 @@ func (p *parser) parseVariable() Expression {
}
value = assignment.Value
}
} else {
value = &NotEvaluated{}
}
value = &Variable{
Name: text,

View file

@ -1122,3 +1122,24 @@ func TestParserEndPos(t *testing.T) {
}
}
}
func TestParserNotEvaluated(t *testing.T) {
// When parsing without evaluation, create variables correctly
scope := NewScope(nil)
input := "FOO=abc\n"
_, errs := Parse("", bytes.NewBufferString(input), scope)
if errs != nil {
t.Errorf("unexpected errors:")
for _, err := range errs {
t.Errorf(" %s", err)
}
t.FailNow()
}
assignment, found := scope.Get("FOO")
if !found {
t.Fatalf("Expected to find FOO after parsing %s", input)
}
if s := assignment.String(); strings.Contains(s, "PANIC") {
t.Errorf("Attempt to print FOO returned %s", s)
}
}