]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/gc: fix uintptr(nil) issues.
authorRémy Oudompheng <oudomphe@phare.normalesup.org>
Mon, 7 Jan 2013 23:23:02 +0000 (00:23 +0100)
committerRémy Oudompheng <oudomphe@phare.normalesup.org>
Mon, 7 Jan 2013 23:23:02 +0000 (00:23 +0100)
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

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

index 31ea3a251cdf2e443038212ff64469f625b0445a..0224665519b4bf127da4aa6585f59357f6173e2b 100644 (file)
@@ -162,6 +162,16 @@ convlit1(Node **np, Type *t, int explicit)
                case TFUNC:
                case TUNSAFEPTR:
                        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;
 
index 1cbda6245b6a73b922c06df28df2a55918e321d1..8b2120253ee0042eba9502da137001c5a51108f3 100644 (file)
@@ -810,7 +810,11 @@ cgen_slice(Node *n, Node *res)
                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.type = types[TUINTPTR];
 
diff --git a/test/fixedbugs/issue4614.go b/test/fixedbugs/issue4614.go
new file mode 100644 (file)
index 0000000..1aa318c
--- /dev/null
@@ -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))