]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/cgo: fix default alignment for empty structs
authorMatthew Dempsky <mdempsky@google.com>
Tue, 12 Aug 2014 05:10:17 +0000 (22:10 -0700)
committerIan Lance Taylor <iant@golang.org>
Tue, 12 Aug 2014 05:10:17 +0000 (22:10 -0700)
Fixes #5242.

LGTM=iant
R=iant
CC=golang-codereviews
https://golang.org/cl/125120043

misc/cgo/test/cgo_test.go
misc/cgo/test/issue5242.go [new file with mode: 0644]
src/cmd/cgo/gcc.go

index eb237725a4f4e49485700c3c30c55ebc5a0b95d9..3c6d5cb59e8fef7d5567b13108143d21743ae9af 100644 (file)
@@ -53,5 +53,6 @@ func Test5986(t *testing.T)                { test5986(t) }
 func Test7665(t *testing.T)                { test7665(t) }
 func TestNaming(t *testing.T)              { testNaming(t) }
 func Test7560(t *testing.T)                { test7560(t) }
+func Test5242(t *testing.T)                { test5242(t) }
 
 func BenchmarkCgoCall(b *testing.B) { benchCgoCall(b) }
diff --git a/misc/cgo/test/issue5242.go b/misc/cgo/test/issue5242.go
new file mode 100644 (file)
index 0000000..fe0a632
--- /dev/null
@@ -0,0 +1,31 @@
+// Copyright 2014 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 5242.  Cgo incorrectly computed the alignment of structs
+// with no Go accessible fields as 0, and then panicked on
+// modulo-by-zero computations.
+
+package cgotest
+
+/*
+typedef struct {
+} foo;
+
+typedef struct {
+       int x : 1;
+} bar;
+
+int issue5242(foo f, bar b) {
+       return 5242;
+}
+*/
+import "C"
+
+import "testing"
+
+func test5242(t *testing.T) {
+       if got := C.issue5242(C.foo{}, C.bar{}); got != 5242 {
+               t.Errorf("got %v", got)
+       }
+}
index aa28060ea77b055b9c56a9fcd0876c9401492d3d..b79725ab01a1bf0b36ed8b93b0074175e36317d9 100644 (file)
@@ -1534,6 +1534,9 @@ func (c *typeConv) pad(fld []*ast.Field, size int64) []*ast.Field {
 
 // Struct conversion: return Go and (6g) C syntax for type.
 func (c *typeConv) Struct(dt *dwarf.StructType, pos token.Pos) (expr *ast.StructType, csyntax string, align int64) {
+       // Minimum alignment for a struct is 1 byte.
+       align = 1
+
        var buf bytes.Buffer
        buf.WriteString("struct {")
        fld := make([]*ast.Field, 0, 2*len(dt.Field)+1) // enough for padding around every field