From: Matt Corallo Date: Mon, 1 Mar 2021 22:07:29 +0000 (-0500) Subject: [bindings] Be explicit when calling pointer.is_null() X-Git-Tag: v0.0.13~6^2~5 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=6b4c8e4690116a3bf41054ccac908bfba0a7af01;p=rust-lightning [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. --- diff --git a/c-bindings-gen/src/main.rs b/c-bindings-gen/src/main.rs index f2f684fd6..a0acfc65d 100644 --- a/c-bindings-gen/src/main.rs +++ b/c-bindings-gen/src/main.rs @@ -536,7 +536,7 @@ fn writeln_opaque(w: &mut W, ident: &syn::Ident, struct_name: writeln!(w, "\t/// the Rust equivalent takes an Option, it may be set to null to indicate None.").unwrap(); writeln!(w, "\tpub inner: *mut native{},\n\tpub is_owned: bool,\n}}\n", ident).unwrap(); writeln!(w, "impl Drop for {} {{\n\tfn drop(&mut self) {{", struct_name).unwrap(); - writeln!(w, "\t\tif self.is_owned && !self.inner.is_null() {{").unwrap(); + writeln!(w, "\t\tif self.is_owned && !<*mut native{}>::is_null(self.inner) {{", ident).unwrap(); writeln!(w, "\t\t\tlet _ = unsafe {{ Box::from_raw(self.inner) }};\n\t\t}}\n\t}}\n}}").unwrap(); writeln!(w, "#[no_mangle]\npub extern \"C\" fn {}_free(this_ptr: {}) {{ }}", struct_name, struct_name).unwrap(); writeln!(w, "#[allow(unused)]").unwrap(); @@ -913,7 +913,7 @@ fn writeln_impl(w: &mut W, i: &syn::ItemImpl, types: &mut Typ writeln!(w, "impl Clone for {} {{", ident).unwrap(); writeln!(w, "\tfn clone(&self) -> Self {{").unwrap(); writeln!(w, "\t\tSelf {{").unwrap(); - writeln!(w, "\t\t\tinner: if self.inner.is_null() {{ std::ptr::null_mut() }} else {{").unwrap(); + writeln!(w, "\t\t\tinner: if <*mut native{}>::is_null(self.inner) {{ std::ptr::null_mut() }} else {{", ident).unwrap(); writeln!(w, "\t\t\t\tBox::into_raw(Box::new(unsafe {{ &*self.inner }}.clone())) }},").unwrap(); writeln!(w, "\t\t\tis_owned: true,").unwrap(); writeln!(w, "\t\t}}\n\t}}\n}}").unwrap();