Permalink
Browse files

rustc: fix size computation of structs for the FFI

It didn't take alignment into account.

Fixes #3656.
  • Loading branch information...
1 parent 0d8f447 commit 25096a212a9ccaa0d181630af5971532c3472182 @Blei Blei committed Oct 10, 2012
Showing with 24 additions and 3 deletions.
  1. +4 −3 src/rustc/middle/trans/foreign.rs
  2. +20 −0 src/test/run-pass/issue-3656.rs
@@ -112,9 +112,10 @@ fn classify_ty(ty: TypeRef) -> ~[x86_64_reg_class] {
Float => 4,
Double => 8,
Struct => {
- do vec::foldl(0, struct_tys(ty)) |s, t| {
- s + ty_size(*t)
- }
+ let size = do vec::foldl(0, struct_tys(ty)) |s, t| {
+ align(s, *t) + ty_size(*t)
+ };
+ align(size, ty)
}
Array => {
let len = llvm::LLVMGetArrayLength(ty) as uint;
@@ -0,0 +1,20 @@
+// Issue #3656
+// Incorrect struct size computation in the FFI, because of not taking
+// the alignment of elements into account.
+
+use libc::*;
+
+struct KEYGEN {
+ hash_algorithm: [c_uint]/2,
+ count: uint32_t,
+ salt: *c_void,
+ salt_size: uint32_t,
+}
+
+extern {
+ // Bogus signature, just need to test if it compiles.
+ pub fn malloc(++data: KEYGEN);
+}
+
+fn main() {
+}

0 comments on commit 25096a2

Please sign in to comment.