Skip to content

Commit

Permalink
cmd/gc: disallow pointer constants
Browse files Browse the repository at this point in the history
Fixes #7760.

LGTM=iant
R=iant, remyoudompheng
CC=golang-codereviews
https://golang.org/cl/130720043
  • Loading branch information
mdempsky authored and ianlancetaylor committed Aug 15, 2014
1 parent fb0b923 commit 7f40e5e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/cmd/gc/const.c
Expand Up @@ -1566,15 +1566,19 @@ isgoconst(Node *n)
case ORSH:
case OSUB:
case OXOR:
case OCONV:
case OIOTA:
case OCOMPLEX:
case OREAL:
case OIMAG:
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;
Expand Down
25 changes: 25 additions & 0 deletions test/fixedbugs/issue7760.go
@@ -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"

0 comments on commit 7f40e5e

Please sign in to comment.