Skip to content

Commit

Permalink
internal/cmd/cue-ast-print: do not panic on invalid values
Browse files Browse the repository at this point in the history
For example, in SliceExpr, the High field can be a nil interface,
and that caused us to panic as reflect.Value.Elem returns a zero Value
in such a scenario. Thanks to Rudolf Farkas for the report and fix.

Note that we don't have any tests right now, as this tool
is used only for debugging purposes.

Closes #2771 as merged.

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: Id41797ea500b75bbc7a09f7f40cc9e51f5c0931a
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1194721
TryBot-Result: CUEcueckoo <[email protected]>
Reviewed-by: Roger Peppe <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
  • Loading branch information
mvdan committed May 15, 2024
1 parent 97695d0 commit c789d3e
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions internal/cmd/cue-ast-print/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,17 @@ func (d *debugPrinter) value(v reflect.Value) {
if v.Kind() == reflect.Interface {
v = v.Elem()
}
// We print the original pointer type if there was one.
origType := v.Type()
v = reflect.Indirect(v)

// Indirecting a nil interface/pointer gives a zero value;
// stop as calling reflect.Value.Type on an invalid type would panic.
if !v.IsValid() {
// Indirecting a nil interface/pointer gives a zero value.
d.printf("nil")
return
}
// We print the original pointer type if there was one.
origType := v.Type()
v = reflect.Indirect(v)

t := v.Type()
switch t {
// Simple types which can stringify themselves.
Expand Down

0 comments on commit c789d3e

Please sign in to comment.