]> git.bitcoin.ninja Git - ldk-c-bindings/commitdiff
Fix double-free of String bytes after converting an owned Str.
authorMatt Corallo <git@bluematt.me>
Mon, 3 May 2021 16:33:05 +0000 (16:33 +0000)
committerMatt Corallo <git@bluematt.me>
Mon, 3 May 2021 16:33:35 +0000 (16:33 +0000)
lightning-c-bindings/src/c_types/mod.rs

index 3d45802e8063850cc2f71983e47daeefce11a304..6267b80bc2dd007ba9476f76d01127d2b9a74937 100644 (file)
@@ -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) });