]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1.12] cmd/cgo: fix inappropriate array copy
authorIan Lance Taylor <iant@golang.org>
Mon, 24 Jun 2019 20:17:30 +0000 (13:17 -0700)
committerIan Lance Taylor <iant@golang.org>
Wed, 26 Jun 2019 18:24:59 +0000 (18:24 +0000)
Ensure that during rewriting of expressions that take the address of
an array, that we properly recognize *ast.IndexExpr as an operation
to create a pointer variable and thus assign the proper addressOf
and deference operators as "&" and "*" respectively.

This fixes a regression from CL 142884.

This is a backport of CLs 183458 and 183778 to the 1.12 release branch.
It is not a cherry pick because the code in misc/cgo/test has changed.

Updates #32579
Fixes #32756

Change-Id: I0daa75ec62cccbe82ab658cb2947f51423e0c235
Reviewed-on: https://go-review.googlesource.com/c/go/+/183627
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
misc/cgo/test/cgo_test.go
misc/cgo/test/issue32579.go [new file with mode: 0644]
src/cmd/cgo/gcc.go

index 2cb93d9c2eb283555bbe486a65d177e021e9e0c0..3c905b8bdf9759fd9f810bf1da631337915e6852 100644 (file)
@@ -95,6 +95,7 @@ func Test26213(t *testing.T)                 { test26213(t) }
 func Test27660(t *testing.T)                 { test27660(t) }
 func Test28896(t *testing.T)                 { test28896(t) }
 func Test30065(t *testing.T)                 { test30065(t) }
+func Test32579(t *testing.T)                 { test32579(t) }
 
 func BenchmarkCgoCall(b *testing.B)  { benchCgoCall(b) }
 func BenchmarkGoString(b *testing.B) { benchGoString(b) }
diff --git a/misc/cgo/test/issue32579.go b/misc/cgo/test/issue32579.go
new file mode 100644 (file)
index 0000000..6776de7
--- /dev/null
@@ -0,0 +1,22 @@
+// Copyright 2019 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.
+
+package cgotest
+
+// #include <string.h>
+// typedef struct S32579 { unsigned char data[1]; } S32579;
+import "C"
+
+import (
+       "testing"
+       "unsafe"
+)
+
+func test32579(t *testing.T) {
+       var s [1]C.struct_S32579
+       C.memset(unsafe.Pointer(&s[0].data[0]), 1, 1)
+       if s[0].data[0] != 1 {
+               t.Errorf("&s[0].data[0] failed: got %d, want %d", s[0].data[0], 1)
+       }
+}
index 915ad66111b8987dad2201ff279dff8a6e716db5..55d9fd9da46654c9ccae11adcc95ffd54340d225 100644 (file)
@@ -1256,6 +1256,8 @@ func (p *Package) isVariable(x ast.Expr) bool {
                return true
        case *ast.SelectorExpr:
                return p.isVariable(x.X)
+       case *ast.IndexExpr:
+               return true
        }
        return false
 }