]> Cypherpunks.ru repositories - gostls13.git/commitdiff
[dev.typeparams] cmd/compile/internal/types2: don't crash if import path is missing
authorRobert Griesemer <gri@golang.org>
Tue, 15 Dec 2020 01:23:00 +0000 (17:23 -0800)
committerRobert Griesemer <gri@golang.org>
Tue, 15 Dec 2020 19:44:38 +0000 (19:44 +0000)
In package syntax:
- fix parser appendGroup to not add nil entries
- non-string paths are syntax errors per the spec; report in parser
- document ImportDecl.Path invariants

In package types2:
- guard against absent paths

In package gc:
- guard against absent paths

Fixes #43190.

Change-Id: Ic6a06f6a96b7f519feaa1ceaf4376fc5ab0f0129
Reviewed-on: https://go-review.googlesource.com/c/go/+/278114
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
src/cmd/compile/internal/gc/noder.go
src/cmd/compile/internal/syntax/nodes.go
src/cmd/compile/internal/syntax/parser.go
src/cmd/compile/internal/types2/fixedbugs/issue43190.src [new file with mode: 0644]
src/cmd/compile/internal/types2/resolver.go

index 8ae88a100c0e46d036a62d6cedf4f0223ee6ad7d..65df405e24d3f138d330382736e7649a4fb05da7 100644 (file)
@@ -463,7 +463,7 @@ func (p *noder) decls(decls []syntax.Decl) (l []ir.Node) {
 }
 
 func (p *noder) importDecl(imp *syntax.ImportDecl) {
-       if imp.Path.Bad {
+       if imp.Path == nil || imp.Path.Bad {
                return // avoid follow-on errors if there was a syntax error
        }
 
index fe8f62c6e6b54ba60ada92601f385eee4c68986b..a06d6e85b1e82987bba07a557a7567794085c801 100644 (file)
@@ -55,8 +55,8 @@ type (
        ImportDecl struct {
                Group        *Group // nil means not part of a group
                Pragma       Pragma
-               LocalPkgName *Name // including "."; nil means no rename present
-               Path         *BasicLit
+               LocalPkgName *Name     // including "."; nil means no rename present
+               Path         *BasicLit // Path.Bad || Path.Kind == StringLit; nil means no path
                decl
        }
 
index 4af7e462ede5f80d2da119f067280aafa348e50e..90b67def0f2037b4cbbe19fc9d1de60c310ce429 100644 (file)
@@ -499,21 +499,16 @@ func (p *parser) appendGroup(list []Decl, f func(*Group) Decl) []Decl {
                p.clearPragma()
                p.next() // must consume "(" after calling clearPragma!
                p.list(_Semi, _Rparen, func() bool {
-                       list = append(list, f(g))
+                       if x := f(g); x != nil {
+                               list = append(list, x)
+                       }
                        return false
                })
        } else {
-               list = append(list, f(nil))
-       }
-
-       if debug {
-               for _, d := range list {
-                       if d == nil {
-                               panic("nil list entry")
-                       }
+               if x := f(nil); x != nil {
+                       list = append(list, x)
                }
        }
-
        return list
 }
 
@@ -540,8 +535,13 @@ func (p *parser) importDecl(group *Group) Decl {
        if d.Path == nil {
                p.syntaxError("missing import path")
                p.advance(_Semi, _Rparen)
-               return nil
+               return d
+       }
+       if !d.Path.Bad && d.Path.Kind != StringLit {
+               p.syntaxError("import path must be a string")
+               d.Path.Bad = true
        }
+       // d.Path.Bad || d.Path.Kind == StringLit
 
        return d
 }
diff --git a/src/cmd/compile/internal/types2/fixedbugs/issue43190.src b/src/cmd/compile/internal/types2/fixedbugs/issue43190.src
new file mode 100644 (file)
index 0000000..ae42719
--- /dev/null
@@ -0,0 +1,19 @@
+// Copyright 2020 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package p
+
+import ; // ERROR missing import path
+import
+var /* ERROR missing import path */ _ int
+import .; // ERROR missing import path
+
+import ()
+import (.) // ERROR missing import path
+import (
+       "fmt"
+       .
+) // ERROR missing import path
+
+var _ = fmt.Println // avoid imported but not used error
index 2e90e5781cc83c3b0e88d1ce36f62639285c54d7..6765c219956078590f85ae07d11cc8daab5cffa0 100644 (file)
@@ -236,7 +236,7 @@ func (check *Checker) collectObjects() {
                        switch s := decl.(type) {
                        case *syntax.ImportDecl:
                                // import package
-                               if s.Path.Bad {
+                               if s.Path == nil || s.Path.Bad {
                                        continue // error reported during parsing
                                }
                                path, err := validatedImportPath(s.Path.Value)