mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
cmd/cgo: omit misaligned struct fields, like we omit bitfields
Fixes #7560. LGTM=iant R=golang-codereviews, iant CC=golang-codereviews https://golang.org/cl/96300045
This commit is contained in:
parent
211618c26e
commit
2d1a9510ed
4 changed files with 60 additions and 3 deletions
|
|
@ -1499,7 +1499,7 @@ func (c *typeConv) Struct(dt *dwarf.StructType, pos token.Pos) (expr *ast.Struct
|
|||
t := c.Type(f.Type, pos)
|
||||
tgo := t.Go
|
||||
size := t.Size
|
||||
|
||||
talign := t.Align
|
||||
if f.BitSize > 0 {
|
||||
if f.BitSize%8 != 0 {
|
||||
continue
|
||||
|
|
@ -1512,8 +1512,17 @@ func (c *typeConv) Struct(dt *dwarf.StructType, pos token.Pos) (expr *ast.Struct
|
|||
name = "uint"
|
||||
}
|
||||
tgo = ast.NewIdent(name + fmt.Sprint(f.BitSize))
|
||||
talign = size
|
||||
}
|
||||
|
||||
if talign > 0 && f.ByteOffset%talign != 0 {
|
||||
// Drop misaligned fields, the same way we drop integer bit fields.
|
||||
// The goal is to make available what can be made available.
|
||||
// Otherwise one bad and unneeded field in an otherwise okay struct
|
||||
// makes the whole program not compile. Much of the time these
|
||||
// structs are in system headers that cannot be corrected.
|
||||
continue
|
||||
}
|
||||
n := len(fld)
|
||||
fld = fld[0 : n+1]
|
||||
name := f.Name
|
||||
|
|
@ -1528,8 +1537,8 @@ func (c *typeConv) Struct(dt *dwarf.StructType, pos token.Pos) (expr *ast.Struct
|
|||
buf.WriteString(" ")
|
||||
buf.WriteString(name)
|
||||
buf.WriteString("; ")
|
||||
if t.Align > align {
|
||||
align = t.Align
|
||||
if talign > align {
|
||||
align = talign
|
||||
}
|
||||
}
|
||||
if off < dt.ByteSize {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue