cmd/gc: disallow pointer constants

Fixes #7760.

LGTM=iant
R=iant, remyoudompheng
CC=golang-codereviews
https://golang.org/cl/130720043
This commit is contained in:
Matthew Dempsky 2014-08-15 11:33:31 -07:00 committed by Ian Lance Taylor
parent fb0b923f67
commit 7f40e5e6e5
2 changed files with 31 additions and 2 deletions

View file

@ -1566,7 +1566,6 @@ isgoconst(Node *n)
case ORSH: case ORSH:
case OSUB: case OSUB:
case OXOR: case OXOR:
case OCONV:
case OIOTA: case OIOTA:
case OCOMPLEX: case OCOMPLEX:
case OREAL: case OREAL:
@ -1574,7 +1573,12 @@ isgoconst(Node *n)
if(isgoconst(n->left) && (n->right == N || isgoconst(n->right))) if(isgoconst(n->left) && (n->right == N || isgoconst(n->right)))
return 1; return 1;
break; break;
case OCONV:
if(okforconst[n->type->etype] && isgoconst(n->left))
return 1;
break;
case OLEN: case OLEN:
case OCAP: case OCAP:
l = n->left; l = n->left;

View file

@ -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"