]> Cypherpunks repositories - gostls13.git/commitdiff
gc: more crash avoidance
authorRuss Cox <rsc@golang.org>
Tue, 27 Jul 2010 00:34:17 +0000 (17:34 -0700)
committerRuss Cox <rsc@golang.org>
Tue, 27 Jul 2010 00:34:17 +0000 (17:34 -0700)
Fixes #961.
Fixes #962.

R=ken2
CC=golang-dev
https://golang.org/cl/1903043

src/cmd/gc/const.c
src/cmd/gc/typecheck.c
test/fixedbugs/bug297.go [new file with mode: 0644]
test/fixedbugs/bug298.go [new file with mode: 0644]

index cec95359a0b7905664708b1559979ab40195a4fe..479e7dd6b717048f18903d8d2b7191460bf59f6b 100644 (file)
@@ -536,6 +536,12 @@ evconst(Node *n)
                v = toflt(v);
                rv = toflt(rv);
        }
+       if(v.ctype != rv.ctype) {
+               // Use of undefined name as constant?
+               if((v.ctype == 0 || rv.ctype == 0) && nerrors > 0)
+                       return;
+               fatal("constant type mismatch %T(%d) %T(%d)", nl->type, v.ctype, nr->type, rv.ctype);
+       }
 
        // run op
        switch(TUP(n->op, v.ctype)) {
index 3784c6699e187b3f34211dc0dbd304a1c15fe194..b1991333ca0641cf40076bc5f87e9249080d66e9 100644 (file)
@@ -1129,6 +1129,10 @@ reswitch:
        case ORETURN:
                ok |= Etop;
                typechecklist(n->list, Erv | Efnstruct);
+               if(curfn == N) {
+                       yyerror("return outside function");
+                       goto error;
+               }
                if(curfn->type->outnamed && n->list == nil)
                        goto ret;
                typecheckaste(ORETURN, getoutargx(curfn->type), n->list, "return argument");
diff --git a/test/fixedbugs/bug297.go b/test/fixedbugs/bug297.go
new file mode 100644 (file)
index 0000000..ba02942
--- /dev/null
@@ -0,0 +1,15 @@
+// errchk $G $D/$F.go
+
+// Copyright 2010 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.
+
+// Used to crash; issue 961.
+
+package main
+
+type ByteSize float64
+const (
+       _ = iota;   // ignore first value by assigning to blank identifier
+       KB ByteSize = 1<<(10*X) // ERROR "undefined"
+)
diff --git a/test/fixedbugs/bug298.go b/test/fixedbugs/bug298.go
new file mode 100644 (file)
index 0000000..9b329ae
--- /dev/null
@@ -0,0 +1,11 @@
+// errchk $G $D/$F.go
+
+// Copyright 2010 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 ddd
+
+func Sum() int
+       for i := range []int{} { return i }  // ERROR "return outside function"
+