Skip to content

Commit

Permalink
Merge pull request #18 from snipsco/fix_cdrop
Browse files Browse the repository at this point in the history
Fix double free for non-pointer type
  • Loading branch information
anthonyray authored Mar 19, 2020
2 parents 52782a9 + df307de commit 26785ee
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
2 changes: 1 addition & 1 deletion ffi-convert-derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ffi-convert-derive"
version = "0.1.0"
version = "0.1.1"
authors = ["Sonos"]
edition = "2018"
license = "MIT OR Apache-2.0"
Expand Down
9 changes: 4 additions & 5 deletions ffi-convert-derive/src/cdrop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ pub fn impl_cdrop_macro(input: &syn::DeriveInput) -> TokenStream {

let drop_field = if field.is_string {
quote!(ffi_convert::take_back_c_string!(self.#field_name))
} else if field.is_pointer {
quote!( unsafe { #field_type::drop_raw_pointer(self.#field_name) }? )
} else {
if field.is_pointer {
quote!( unsafe { #field_type::drop_raw_pointer(self.#field_name) }? )
} else {
quote!( self.# field_name.do_drop()? )
}
// the other cases will be handled automatically by rust
quote!()
};

let conversion = if field.is_nullable {
Expand Down
4 changes: 2 additions & 2 deletions ffi-convert-tests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[package]
name = "ffi-convert-tests"
version = "0.1.0"
version = "0.1.1"
authors = ["Sonos"]
edition = "2018"

[dependencies]
failure = "0.1"
ffi-convert = "0.1"
ffi-convert = "0.1.1"
libc = "0.2.66"
19 changes: 16 additions & 3 deletions ffi-convert-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,15 @@ pub struct CLayer {
#[derive(Clone, Debug, PartialEq)]
pub struct Dummy {
pub count: i32,
pub describe: String,
}

#[repr(C)]
#[derive(CReprOf, AsRust, CDrop)]
#[target_type(Dummy)]
pub struct CDummy {
count: i32,
describe: *const libc::c_char,
}

#[cfg(test)]
Expand All @@ -123,7 +125,12 @@ mod tests {
Topping { amount: 2 }
});

generate_round_trip_rust_c_rust!(round_trip_dummy, Dummy, CDummy, { Dummy { count: 2 } });
generate_round_trip_rust_c_rust!(round_trip_dummy, Dummy, CDummy, {
Dummy {
count: 2,
describe: "yo".to_string(),
}
});

generate_round_trip_rust_c_rust!(round_trip_layer, Layer, CLayer, {
Layer {
Expand All @@ -138,7 +145,10 @@ mod tests {
description: Some("I'm delicious ! ".to_string()),
start: 0.0,
end: Some(2.0),
dummy: Dummy { count: 2 },
dummy: Dummy {
count: 2,
describe: "yo".to_string(),
},
sauce: Some(Sauce { volume: 32.23 }),
toppings: vec![Topping { amount: 2 }, Topping { amount: 3 }],
layers: Some(vec![Layer {
Expand All @@ -155,7 +165,10 @@ mod tests {
description: Some("I'm delicious ! ".to_string()),
start: 0.0,
end: None,
dummy: Dummy { count: 2 },
dummy: Dummy {
count: 2,
describe: "yo".to_string(),
},
sauce: None,
toppings: vec![],
layers: Some(vec![]),
Expand Down
4 changes: 2 additions & 2 deletions ffi-convert/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ffi-convert"
version = "0.1.0"
version = "0.1.1"
authors = ["Sonos"]
edition = "2018"
license = "MIT OR Apache-2.0"
Expand All @@ -10,6 +10,6 @@ readme = "../README.md"
keywords = ["ffi"]

[dependencies]
ffi-convert-derive = "0.1"
ffi-convert-derive = "0.1.1"
failure = "0.1"
libc = "0.2"

0 comments on commit 26785ee

Please sign in to comment.