Skip to content

Commit

Permalink
Fix a nil deref, take two (#245)
Browse files Browse the repository at this point in the history
The change in 0d2eda1 did not fix the panic condition, which occurs if the underlying *StringExpr is nil. Check for nil using reflection, and add a test to verify the fix.
  • Loading branch information
lblackstone authored Feb 6, 2024
1 parent 0d2eda1 commit ae63718
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
3 changes: 2 additions & 1 deletion ast/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package ast
import (
"encoding/json"
"fmt"
"reflect"
"strings"

"github.com/hashicorp/hcl/v2"
Expand Down Expand Up @@ -59,7 +60,7 @@ type exprConstraint interface {

func exprPosition[T exprConstraint](expr T) (*hcl.Range, string) {
var zero T
if expr != zero {
if expr != zero && !reflect.ValueOf(expr).IsNil() {
if syntax := expr.Syntax(); syntax != nil {
return syntax.Syntax().Range(), syntax.Syntax().Path()
}
Expand Down
45 changes: 45 additions & 0 deletions ast/expr_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2024, Pulumi Corporation.
//
// 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 ast

import (
"testing"

"github.com/pulumi/esc/syntax"
"github.com/stretchr/testify/assert"
)

func TestExprError(t *testing.T) {
type args struct {
expr Expr
summary string
}
var ss *StringExpr
tests := []struct {
name string
args args
want *syntax.Diagnostic
}{
{"nil *StringExpr",
args{
ss, "",
}, syntax.Error(nil, "", "")},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, ExprError(tt.args.expr, tt.args.summary), "ExprError(%v, %v)", tt.args.expr, tt.args.summary)
})
}
}

0 comments on commit ae63718

Please sign in to comment.