Skip to content

Commit

Permalink
internal/astinternal: don't panic on nil pointers in DebugPrint
Browse files Browse the repository at this point in the history
For example, on an unnamed import, whose ImportSpec.Name *Ident is nil.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: Ica114ad96defb35fa8b86b31c9686d2e5750c066
Dispatch-Trailer: {"type":"trybot","CL":1199936,"patchset":1,"ref":"refs/changes/36/1199936/1","targetBranch":"master"}
  • Loading branch information
mvdan authored and cueckoo committed Aug 23, 2024
1 parent 59489a7 commit cf0e8c9
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 3 deletions.
11 changes: 8 additions & 3 deletions internal/astinternal/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,21 @@ func (d *debugPrinter) value(v reflect.Value, impliedType reflect.Type) {
if v.Kind() == reflect.Interface {
v = v.Elem()
}

// Indirecting a nil interface/pointer gives a zero value;
// stop as calling reflect.Value.Type on an invalid type would panic.
// Indirecting a nil interface gives a zero value.
if !v.IsValid() {
d.printf("nil")
return
}

// We print the original pointer type if there was one.
origType := v.Type()

v = reflect.Indirect(v)
// Indirecting a nil pointer gives a zero value.
if !v.IsValid() {
d.printf("nil")
return
}

t := v.Type()
switch t {
Expand Down
98 changes: 98 additions & 0 deletions internal/astinternal/testdata/debugprint/file.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ package p
-- comments_only.cue --
// some
// comment lines
-- imports.cue --
package p

import "foo"

import (
"bar"
name "baz"
)
-- out/debugprint/empty.cue --
*ast.File{
Filename: "empty.cue"
Expand Down Expand Up @@ -47,3 +56,92 @@ package p
}
}
}
-- out/debugprint/imports.cue --
*ast.File{
Filename: "imports.cue"
Decls: []ast.Decl{
*ast.Package{
PackagePos: token.Pos("imports.cue:1:1")
Name: *ast.Ident{
NamePos: token.Pos("imports.cue:1:9")
Name: "p"
}
}
*ast.ImportDecl{
Import: token.Pos("imports.cue:3:1")
Lparen: token.Pos("-")
Specs: []*ast.ImportSpec{
{
Name: nil
Path: *ast.BasicLit{
ValuePos: token.Pos("imports.cue:3:8")
Kind: token.Token("STRING")
Value: "\"foo\""
}
EndPos: token.Pos("-")
}
}
Rparen: token.Pos("-")
}
*ast.ImportDecl{
Import: token.Pos("imports.cue:5:1")
Lparen: token.Pos("imports.cue:5:8")
Specs: []*ast.ImportSpec{
{
Name: nil
Path: *ast.BasicLit{
ValuePos: token.Pos("imports.cue:6:2")
Kind: token.Token("STRING")
Value: "\"bar\""
}
EndPos: token.Pos("-")
}
{
Name: *ast.Ident{
NamePos: token.Pos("imports.cue:7:2")
Name: "name"
}
Path: *ast.BasicLit{
ValuePos: token.Pos("imports.cue:7:7")
Kind: token.Token("STRING")
Value: "\"baz\""
}
EndPos: token.Pos("-")
}
}
Rparen: token.Pos("imports.cue:8:1")
}
}
Imports: []*ast.ImportSpec{
{
Name: nil
Path: *ast.BasicLit{
ValuePos: token.Pos("imports.cue:3:8")
Kind: token.Token("STRING")
Value: "\"foo\""
}
EndPos: token.Pos("-")
}
{
Name: nil
Path: *ast.BasicLit{
ValuePos: token.Pos("imports.cue:6:2")
Kind: token.Token("STRING")
Value: "\"bar\""
}
EndPos: token.Pos("-")
}
{
Name: *ast.Ident{
NamePos: token.Pos("imports.cue:7:2")
Name: "name"
}
Path: *ast.BasicLit{
ValuePos: token.Pos("imports.cue:7:7")
Kind: token.Token("STRING")
Value: "\"baz\""
}
EndPos: token.Pos("-")
}
}
}

0 comments on commit cf0e8c9

Please sign in to comment.