1
0
mirror of https://github.com/golang/go synced 2024-11-26 04:37:59 -07:00

cmd/gc: fix uintptr(nil) issues.

A constant node of type uintptr with a nil literal could
happen in two cases: []int(nil)[1:] and
uintptr(unsafe.Pointer(nil)).

Fixes #4614.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/7059043
This commit is contained in:
Rémy Oudompheng 2013-01-08 00:23:02 +01:00
parent 77c343328e
commit fba96e915d
3 changed files with 35 additions and 1 deletions

View File

@ -162,6 +162,16 @@ convlit1(Node **np, Type *t, int explicit)
case TFUNC: case TFUNC:
case TUNSAFEPTR: case TUNSAFEPTR:
break; break;
case TUINTPTR:
// A nil literal may be converted to uintptr
// if it is an unsafe.Pointer
if(n->type->etype == TUNSAFEPTR) {
n->val.u.xval = mal(sizeof(*n->val.u.xval));
mpmovecfix(n->val.u.xval, 0);
n->val.ctype = CTINT;
} else
goto bad;
} }
break; break;

View File

@ -810,7 +810,11 @@ cgen_slice(Node *n, Node *res)
checkref(n->left); checkref(n->left);
} }
src = *n->left; if(isnil(n->left)) {
tempname(&src, n->left->type);
cgen(n->left, &src);
} else
src = *n->left;
src.xoffset += Array_array; src.xoffset += Array_array;
src.type = types[TUINTPTR]; src.type = types[TUINTPTR];

View File

@ -0,0 +1,20 @@
// compile
// Copyright 2012 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.
// Issue 4614: slicing of nil slices confuses the compiler
// with a uintptr(nil) node.
package p
import "unsafe"
var n int
var _ = []int(nil)[1:]
var _ = []int(nil)[n:]
var _ = uintptr(unsafe.Pointer(nil))
var _ = unsafe.Pointer(uintptr(0))