Skip to content

Commit 03a6a24

Browse files
committed
[bindings] Allow cloning opaque types when inner is NULL
Previously we'd segfault trying to deref the NULL page, but there is no reason to not simply clone by creating another opaque instance with a null inner. This comes up specifically when cloning ChannelSigners as the pubkeys instance is NULL on construction before get_pubkeys is called.
1 parent ac70f27 commit 03a6a24

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

c-bindings-gen/src/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,8 @@ fn writeln_opaque<W: std::io::Write>(w: &mut W, ident: &syn::Ident, struct_name:
559559
writeln!(w, "impl Clone for {} {{", struct_name).unwrap();
560560
writeln!(w, "\tfn clone(&self) -> Self {{").unwrap();
561561
writeln!(w, "\t\tSelf {{").unwrap();
562-
writeln!(w, "\t\t\tinner: Box::into_raw(Box::new(unsafe {{ &*self.inner }}.clone())),").unwrap();
562+
writeln!(w, "\t\t\tinner: if self.inner.is_null() {{ std::ptr::null_mut() }} else {{").unwrap();
563+
writeln!(w, "\t\t\t\tBox::into_raw(Box::new(unsafe {{ &*self.inner }}.clone())) }},").unwrap();
563564
writeln!(w, "\t\t\tis_owned: true,").unwrap();
564565
writeln!(w, "\t\t}}\n\t}}\n}}").unwrap();
565566
writeln!(w, "#[allow(unused)]").unwrap();
@@ -569,7 +570,7 @@ fn writeln_opaque<W: std::io::Write>(w: &mut W, ident: &syn::Ident, struct_name:
569570
writeln!(w, "}}").unwrap();
570571
writeln!(w, "#[no_mangle]").unwrap();
571572
writeln!(w, "pub extern \"C\" fn {}_clone(orig: &{}) -> {} {{", struct_name, struct_name, struct_name).unwrap();
572-
writeln!(w, "\t{} {{ inner: Box::into_raw(Box::new(unsafe {{ &*orig.inner }}.clone())), is_owned: true }}", struct_name).unwrap();
573+
writeln!(w, "\torig.clone()").unwrap();
573574
writeln!(w, "}}").unwrap();
574575
}
575576

0 commit comments

Comments
 (0)