]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/gc: disallow pointer constants
authorMatthew Dempsky <mdempsky@google.com>
Fri, 15 Aug 2014 18:33:31 +0000 (11:33 -0700)
committerIan Lance Taylor <iant@golang.org>
Fri, 15 Aug 2014 18:33:31 +0000 (11:33 -0700)
Fixes #7760.

LGTM=iant
R=iant, remyoudompheng
CC=golang-codereviews
https://golang.org/cl/130720043

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

index c01784a81bc5cb82654f3211f7fe19b9e4d8bffd..e418b9c561e9052a59adbb91e04d3b149700e94e 100644 (file)
@@ -1566,7 +1566,6 @@ isgoconst(Node *n)
        case ORSH:
        case OSUB:
        case OXOR:
-       case OCONV:
        case OIOTA:
        case OCOMPLEX:
        case OREAL:
@@ -1574,7 +1573,12 @@ isgoconst(Node *n)
                if(isgoconst(n->left) && (n->right == N || isgoconst(n->right)))
                        return 1;
                break;
-       
+
+       case OCONV:
+               if(okforconst[n->type->etype] && isgoconst(n->left))
+                       return 1;
+               break;
+
        case OLEN:
        case OCAP:
                l = n->left;
diff --git a/test/fixedbugs/issue7760.go b/test/fixedbugs/issue7760.go
new file mode 100644 (file)
index 0000000..cccae48
--- /dev/null
@@ -0,0 +1,25 @@
+// errorcheck
+
+// Copyright 2014 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.
+
+// Verify that pointers can't be used as constants.
+
+package main
+
+import "unsafe"
+
+type myPointer unsafe.Pointer
+
+const _ = unsafe.Pointer(uintptr(1)) // ERROR "is not (a )?constant"
+const _ = myPointer(uintptr(1)) // ERROR "is not (a )?constant"
+
+const _ = (*int)(unsafe.Pointer(uintptr(1))) // ERROR "is not (a )?constant"
+const _ = (*int)(myPointer(uintptr(1))) // ERROR "is not (a )?constant"
+
+const _ = uintptr(unsafe.Pointer(uintptr(1))) // ERROR "is not (a )?constant"
+const _ = uintptr(myPointer(uintptr(1))) // ERROR "is not (a )?constant"
+
+const _ = []byte("") // ERROR "is not (a )?constant"
+const _ = []rune("") // ERROR "is not (a )?constant"