1
0
mirror of https://github.com/golang/go synced 2024-11-26 23:31:24 -07:00

cmd/cgo: fix default alignment for empty structs

Fixes #5242.

LGTM=iant
R=iant
CC=golang-codereviews
https://golang.org/cl/125120043
This commit is contained in:
Matthew Dempsky 2014-08-11 22:10:17 -07:00 committed by Ian Lance Taylor
parent 03e6a88ef0
commit 31a996edb6
3 changed files with 35 additions and 0 deletions

View 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) }

View File

@ -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)
}
}

View 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