From dd7ec693a5f3d1ac81bfbdfe20c99c3ff8c5532c Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Tue, 7 Apr 2015 17:53:12 +0200 Subject: [PATCH 1/6] Remove some unnecessary transmute calls. --- ports/cef/wrappers.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/ports/cef/wrappers.rs b/ports/cef/wrappers.rs index 3bfd1c27464..d955aac62b0 100644 --- a/ports/cef/wrappers.rs +++ b/ports/cef/wrappers.rs @@ -61,7 +61,7 @@ macro_rules! cef_pointer_wrapper( rust_object } unsafe fn to_rust(c_object: *const $ty) -> &'a $ty { - mem::transmute::<*const $ty,&'a $ty>(c_object) + &*c_object } } impl<'a> CefWrap<*mut $ty> for &'a mut $ty { @@ -69,7 +69,7 @@ macro_rules! cef_pointer_wrapper( rust_object } unsafe fn to_rust(c_object: *mut $ty) -> &'a mut $ty { - mem::transmute::<*mut $ty,&'a mut $ty>(c_object) + &mut *c_object } } cef_noop_wrapper!(*const $ty); @@ -214,8 +214,7 @@ impl<'a> CefWrap<*mut cef_string_t> for &'a mut [u16] { panic!("unimplemented CEF type conversion: &'a str") } unsafe fn to_rust(_: *mut cef_string_t) -> &'a mut [u16] { - mem::transmute::<(int,int),_>(panic!("unimplemented CEF type conversion: *mut \ - cef_string_t")) + panic!("unimplemented CEF type conversion: *mut cef_string_t") } } From b5d41aa8a5171a65a060df62abb5847a98ab334a Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Tue, 7 Apr 2015 17:54:16 +0200 Subject: [PATCH 2/6] Use boxed::into_raw where it makes sense. --- ports/cef/wrappers.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/ports/cef/wrappers.rs b/ports/cef/wrappers.rs index d955aac62b0..f1da9106df2 100644 --- a/ports/cef/wrappers.rs +++ b/ports/cef/wrappers.rs @@ -32,6 +32,7 @@ use types::{cef_window_info_t, cef_xml_encoding_type_t, cef_xml_node_type_t}; use unicode::str::Utf16Encoder; use libc::{self, c_char, c_int, c_ushort, c_void}; +use std::boxed; use std::collections::HashMap; use std::mem; use std::ptr; @@ -187,14 +188,11 @@ impl<'a> CefWrap<*const cef_string_t> for &'a [u16] { // FIXME(pcwalton): This leaks!! We should instead have the caller pass some scratch // stack space to create the object in. What a botch. - let boxed_string = box cef_string_utf16 { + boxed::into_raw(box cef_string_utf16 { str: ptr, length: buffer.len() as u64, dtor: Some(free_boxed_utf16_string as extern "C" fn(*mut c_ushort)), - }; - let result: *const cef_string_utf16 = &*boxed_string; - mem::forget(boxed_string); - result + }) as *const _ } } unsafe fn to_rust(cef_string: *const cef_string_t) -> &'a [u16] { From cba3b6806e664942846cfef7204161addcc4718e Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Tue, 7 Apr 2015 17:54:36 +0200 Subject: [PATCH 3/6] Use raw::Slice where it makes sense. --- ports/cef/wrappers.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/ports/cef/wrappers.rs b/ports/cef/wrappers.rs index f1da9106df2..d8929352c84 100644 --- a/ports/cef/wrappers.rs +++ b/ports/cef/wrappers.rs @@ -36,6 +36,7 @@ use std::boxed; use std::collections::HashMap; use std::mem; use std::ptr; +use std::raw; pub trait CefWrap { fn to_c(rust_object: Self) -> CObject; @@ -196,8 +197,10 @@ impl<'a> CefWrap<*const cef_string_t> for &'a [u16] { } } unsafe fn to_rust(cef_string: *const cef_string_t) -> &'a [u16] { - let (ptr, len): (*mut c_ushort, uint) = ((*cef_string).str, (*cef_string).length as uint); - mem::transmute((ptr, len)) + mem::transmute(raw::Slice { + data: (*cef_string).str, + len: (*cef_string).length as usize, + }) } } From 1fa1950ab1d185d5d310e413a779563b03585d5b Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Tue, 7 Apr 2015 17:55:49 +0200 Subject: [PATCH 4/6] Allocate only as much as necessary. --- ports/cef/wrappers.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/cef/wrappers.rs b/ports/cef/wrappers.rs index d8929352c84..9964eac6728 100644 --- a/ports/cef/wrappers.rs +++ b/ports/cef/wrappers.rs @@ -246,7 +246,7 @@ impl<'a> CefWrap for String { let boxed_string; unsafe { let buffer = libc::malloc((mem::size_of::() as u64) * - ((utf16_chars.len() + 1) as u64 + 1)) as *mut u16; + ((utf16_chars.len() + 1) as u64)) as *mut u16; for (i, ch) in utf16_chars.iter().enumerate() { *buffer.offset(i as int) = *ch } From 6d1cde22830f538b2c36428dfbc19d0839f13d2b Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Tue, 7 Apr 2015 17:56:01 +0200 Subject: [PATCH 5/6] Stop leaking the Vec. --- ports/cef/wrappers.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/ports/cef/wrappers.rs b/ports/cef/wrappers.rs index 9964eac6728..29a04e38678 100644 --- a/ports/cef/wrappers.rs +++ b/ports/cef/wrappers.rs @@ -257,7 +257,6 @@ impl<'a> CefWrap for String { ptr::write(&mut (*boxed_string).str, buffer); ptr::write(&mut (*boxed_string).length, utf16_chars.len() as u64); ptr::write(&mut (*boxed_string).dtor, Some(free_utf16_buffer as extern "C" fn(*mut c_ushort))); - mem::forget(utf16_chars); } boxed_string } From 9ddaf82d47d6a49643885f8bca31d332fd419de9 Mon Sep 17 00:00:00 2001 From: Ms2ger Date: Tue, 7 Apr 2015 17:56:46 +0200 Subject: [PATCH 6/6] Use size_t for the arguments to malloc. --- ports/cef/wrappers.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ports/cef/wrappers.rs b/ports/cef/wrappers.rs index 29a04e38678..efc8ac8aaae 100644 --- a/ports/cef/wrappers.rs +++ b/ports/cef/wrappers.rs @@ -245,14 +245,14 @@ impl<'a> CefWrap for String { let boxed_string; unsafe { - let buffer = libc::malloc((mem::size_of::() as u64) * - ((utf16_chars.len() + 1) as u64)) as *mut u16; + let buffer = libc::malloc((mem::size_of::() as libc::size_t) * + ((utf16_chars.len() + 1) as libc::size_t)) as *mut u16; for (i, ch) in utf16_chars.iter().enumerate() { *buffer.offset(i as int) = *ch } *buffer.offset(utf16_chars.len() as int) = 0; - boxed_string = libc::malloc(mem::size_of::() as u64) as + boxed_string = libc::malloc(mem::size_of::() as libc::size_t) as *mut cef_string_utf16; ptr::write(&mut (*boxed_string).str, buffer); ptr::write(&mut (*boxed_string).length, utf16_chars.len() as u64);