aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@golang.org>2019-06-24 13:17:30 -0700
committerIan Lance Taylor <iant@golang.org>2019-06-26 18:24:59 +0000
commit77c14d2973aa9365bf07ebe9b2c17585f8cece0c (patch)
tree198993a9c01a0ed4075b6e580e2e8418a6e5fffd
parent4ce6a8e89668b87dce67e2f55802903d6eb9110a (diff)
downloadgo-77c14d2973aa9365bf07ebe9b2c17585f8cece0c.tar.gz
go-77c14d2973aa9365bf07ebe9b2c17585f8cece0c.zip
[release-branch.go1.12] cmd/cgo: fix inappropriate array copy
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>
-rw-r--r--misc/cgo/test/cgo_test.go1
-rw-r--r--misc/cgo/test/issue32579.go22
-rw-r--r--src/cmd/cgo/gcc.go2
3 files changed, 25 insertions, 0 deletions
diff --git a/misc/cgo/test/cgo_test.go b/misc/cgo/test/cgo_test.go
index 2cb93d9c2e..3c905b8bdf 100644
--- a/misc/cgo/test/cgo_test.go
+++ b/misc/cgo/test/cgo_test.go
@@ -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
index 0000000000..6776de7cf9
--- /dev/null
+++ b/misc/cgo/test/issue32579.go
@@ -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)
+ }
+}
diff --git a/src/cmd/cgo/gcc.go b/src/cmd/cgo/gcc.go
index 915ad66111..55d9fd9da4 100644
--- a/src/cmd/cgo/gcc.go
+++ b/src/cmd/cgo/gcc.go
@@ -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
}