From 77c14d2973aa9365bf07ebe9b2c17585f8cece0c Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Mon, 24 Jun 2019 13:17:30 -0700 Subject: [PATCH] [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 TryBot-Result: Gobot Gobot Reviewed-by: Emmanuel Odeke --- misc/cgo/test/cgo_test.go | 1 + misc/cgo/test/issue32579.go | 22 ++++++++++++++++++++++ src/cmd/cgo/gcc.go | 2 ++ 3 files changed, 25 insertions(+) create mode 100644 misc/cgo/test/issue32579.go 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 +// 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 }