From: Matt Corallo Date: Mon, 3 May 2021 16:33:05 +0000 (+0000) Subject: Fix double-free of String bytes after converting an owned Str. X-Git-Tag: v0.0.98~7^2 X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=cf371006659446e248fd0625228a5aa0a999e012;p=ldk-c-bindings Fix double-free of String bytes after converting an owned Str. --- diff --git a/lightning-c-bindings/src/c_types/mod.rs b/lightning-c-bindings/src/c_types/mod.rs index 3d45802..6267b80 100644 --- a/lightning-c-bindings/src/c_types/mod.rs +++ b/lightning-c-bindings/src/c_types/mod.rs @@ -387,13 +387,15 @@ impl Str { if self.len == 0 { return ""; } std::str::from_utf8(unsafe { std::slice::from_raw_parts(self.chars, self.len) }).unwrap() } - pub(crate) fn into_string(self) -> String { + pub(crate) fn into_string(mut self) -> String { let bytes = if self.len == 0 { Vec::new() } else if self.chars_is_owned { - unsafe { + let ret = unsafe { Box::from_raw(std::slice::from_raw_parts_mut(unsafe { self.chars as *mut u8 }, self.len)) - }.into() + }.into(); + self.chars_is_owned = false; + ret } else { let mut ret = Vec::with_capacity(self.len); ret.extend_from_slice(unsafe { std::slice::from_raw_parts(self.chars, self.len) });