haskell / c2hs

c2hs is a pre-processor for Haskell FFI bindings to C libraries
http://hackage.haskell.org/package/c2hs
Other
198 stars 50 forks source link

incorrect sizeof for structs with padding at the end #156

Closed eboasson closed 8 years ago

eboasson commented 8 years ago

It appears the padding of a struct to make its size a multiple of its alignment doesn't happen. This also means embedding a struct for which this happens in another struct causes any subsequent fields to have incorrect offsets. Attached is a simple case that triggers it.

A minor change makes this case (and my actual problem go away):

diff --git a/src/C2HS/Gen/Bind.hs b/src/C2HS/Gen/Bind.hs
index 03b5f53..e176d06 100644
--- a/src/C2HS/Gen/Bind.hs
+++ b/src/C2HS/Gen/Bind.hs
@@ -2420,7 +2420,7 @@ sizeAlignOfStructPad decls tag =
   do
     (sz, align) <- sizeAlignOfStruct decls tag
     let b = size CIntPT
-    return (alignOffset sz b b, align)
+    return (alignOffset sz align b, align)

 -- | compute the size and alignment constraint of a given C declaration
 --

bug.zip

takano-akio commented 8 years ago

I also encountered the same problem, and @eboasson's patch above fixed the issue.

ian-ross commented 8 years ago

@eboasson Thanks for the report. I'll try to look at this this weekend.

ian-ross commented 8 years ago

Thanks for this: the fix turned out to involve a tiny bit more (mostly to do with sizing for bitfields), but I've added a test case to make sure this doesn't break again.