Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Go: Do not panic when extracting an object without a parent scope #16865

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions go/extractor/trap/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package trap
import (
"fmt"
"go/types"
"log"

"github.com/github/codeql-go/extractor/util"
)
Expand Down Expand Up @@ -119,15 +120,12 @@ func (l *Labeler) ScopeID(scope *types.Scope, pkg *types.Package) Label {
func (l *Labeler) LookupObjectID(object types.Object, typelbl Label) (Label, bool) {
label, exists := l.objectLabels[object]
if !exists {
if object.Parent() == nil {
// blank identifiers and the pseudo-package `.` (from `import . "..."` imports) can only be referenced
// once, so we can use a fresh label for them
if object.Name() == "_" || object.Name() == "." {
label = l.FreshID()
l.objectLabels[object] = label
return label, false
}
label = InvalidLabel
// blank identifiers and the pseudo-package `.` (from `import . "..."` imports) do not have
// a parent scope, so we have to deal with them separately. They can only be referenced
// once, so we can use a fresh label for them.
if object.Parent() == nil && (object.Name() == "_" || object.Name() == ".") {
label = l.FreshID()
l.objectLabels[object] = label
} else {
label, exists = l.ScopedObjectID(object, func() Label { return typelbl })
}
Expand All @@ -147,20 +145,25 @@ func (l *Labeler) LookupObjectID(object types.Object, typelbl Label) (Label, boo
func (l *Labeler) ScopedObjectID(object types.Object, getTypeLabel func() Label) (Label, bool) {
label, exists := l.objectLabels[object]
if !exists {
scope := object.Parent()
if scope == nil {
panic(fmt.Sprintf("Object has no scope: %v :: %v.\n", object,
l.tw.Package.Fset.Position(object.Pos())))
if meth := findMethodWithGivenReceiver(object); meth != nil {
// associate method receiver objects to special keys, because those can be
// referenced from other files via their method
methlbl, _ := l.MethodID(meth, getTypeLabel())
label, _ = l.ReceiverObjectID(object, methlbl)
} else {
if meth := findMethodWithGivenReceiver(object); meth != nil {
// associate method receiver objects to special keys, because those can be
// referenced from other files via their method
methlbl, _ := l.MethodID(meth, getTypeLabel())
label, _ = l.ReceiverObjectID(object, methlbl)
scope := object.Parent()
var scopeLbl Label
if scope == nil {
// Something has gone wrong if we get here, but we can't panic because
// we want extraction to be deterministic, to avoid alerts appearing and
// disappearing.
log.Printf("Error: Object has no scope: %v :: %v.\n", object,
l.tw.Package.Fset.Position(object.Pos()))
scopeLbl = l.FreshID()
} else {
scopeLbl := l.ScopeID(scope, object.Pkg())
label = l.GlobalID(fmt.Sprintf("{%v},%s;object", scopeLbl, object.Name()))
scopeLbl = l.ScopeID(scope, object.Pkg())
}
label = l.GlobalID(fmt.Sprintf("{%v},%s;object", scopeLbl, object.Name()))
}
l.objectLabels[object] = label
}
Expand Down
Loading