]> git.bitcoin.ninja Git - rust-lightning/commitdiff
[bindings] Be explicit when calling pointer.is_null()
authorMatt Corallo <git@bluematt.me>
Mon, 1 Mar 2021 22:07:29 +0000 (17:07 -0500)
committerMatt Corallo <git@bluematt.me>
Sun, 7 Mar 2021 18:06:07 +0000 (13:06 -0500)
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.

c-bindings-gen/src/main.rs

index f2f684fd64e985179af6ec5839374caa6f2da142..a0acfc65dd6a27cc9e30a0f0167210526ca434bd 100644 (file)
@@ -536,7 +536,7 @@ fn writeln_opaque<W: std::io::Write>(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: std::io::Write>(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();