From cf371006659446e248fd0625228a5aa0a999e012 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Mon, 3 May 2021 16:33:05 +0000 Subject: [PATCH] Fix double-free of String bytes after converting an owned Str. --- lightning-c-bindings/src/c_types/mod.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) 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) }); -- 2.39.5