Skip to content

Commit 9424734

Browse files
committed
[bindings] Be explicit when calling pointer.is_null()
When the (somewhat anti-pattern) `impl Deref for X { type Target = X; .. }` is used, this avoids an infinite dereference exception trying to figure out what type to resolve `is_null` against.
1 parent a074dd4 commit 9424734

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

c-bindings-gen/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ fn writeln_opaque<W: std::io::Write>(w: &mut W, ident: &syn::Ident, struct_name:
536536
writeln!(w, "\t/// the Rust equivalent takes an Option, it may be set to null to indicate None.").unwrap();
537537
writeln!(w, "\tpub inner: *mut native{},\n\tpub is_owned: bool,\n}}\n", ident).unwrap();
538538
writeln!(w, "impl Drop for {} {{\n\tfn drop(&mut self) {{", struct_name).unwrap();
539-
writeln!(w, "\t\tif self.is_owned && !self.inner.is_null() {{").unwrap();
539+
writeln!(w, "\t\tif self.is_owned && !<*mut native{}>::is_null(self.inner) {{", ident).unwrap();
540540
writeln!(w, "\t\t\tlet _ = unsafe {{ Box::from_raw(self.inner) }};\n\t\t}}\n\t}}\n}}").unwrap();
541541
writeln!(w, "#[no_mangle]\npub extern \"C\" fn {}_free(this_ptr: {}) {{ }}", struct_name, struct_name).unwrap();
542542
writeln!(w, "#[allow(unused)]").unwrap();
@@ -913,7 +913,7 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, i: &syn::ItemImpl, types: &mut Typ
913913
writeln!(w, "impl Clone for {} {{", ident).unwrap();
914914
writeln!(w, "\tfn clone(&self) -> Self {{").unwrap();
915915
writeln!(w, "\t\tSelf {{").unwrap();
916-
writeln!(w, "\t\t\tinner: if self.inner.is_null() {{ std::ptr::null_mut() }} else {{").unwrap();
916+
writeln!(w, "\t\t\tinner: if <*mut native{}>::is_null(self.inner) {{ std::ptr::null_mut() }} else {{", ident).unwrap();
917917
writeln!(w, "\t\t\t\tBox::into_raw(Box::new(unsafe {{ &*self.inner }}.clone())) }},").unwrap();
918918
writeln!(w, "\t\t\tis_owned: true,").unwrap();
919919
writeln!(w, "\t\t}}\n\t}}\n}}").unwrap();

0 commit comments

Comments
 (0)