Bug 1344209: Handle empty strings gracefully in rust nsString bindings. r=mystor

MozReview-Commit-ID: 5lI8LXwZIML
Signed-off-by: Emilio Cobos Álvarez <emilio@crisal.io>
This commit is contained in:
Emilio Cobos Álvarez 2017-03-03 15:20:39 +01:00
parent 8681dbd17f
commit 30bd5679fd
3 changed files with 16 additions and 1 deletions

View File

@ -129,3 +129,8 @@ extern "C" void Rust_StringWrite();
TEST(RustNsString, StringWrite) {
Rust_StringWrite();
}
extern "C" void Rust_FromEmptyRustString();
TEST(RustNsString, FromEmptyRustString) {
Rust_FromEmptyRustString();
}

View File

@ -110,3 +110,9 @@ pub extern fn Rust_StringWrite() {
expect_eq!(cs, "abc123");
}
#[no_mangle]
pub extern fn Rust_FromEmptyRustString() {
let mut test = nsString::from("Blah");
test.assign_utf8(&nsCString::from(String::new()));
assert!(test.is_empty());
}

View File

@ -313,7 +313,7 @@ macro_rules! define_string_types {
assert!(s.len() < (u32::MAX as usize));
$String {
hdr: $StringRepr {
data: s.as_ptr(),
data: if s.is_empty() { ptr::null() } else { s.as_ptr() },
length: s.len() as u32,
flags: F_NONE,
},
@ -325,6 +325,10 @@ macro_rules! define_string_types {
impl From<Box<[$char_t]>> for $String<'static> {
fn from(s: Box<[$char_t]>) -> $String<'static> {
assert!(s.len() < (u32::MAX as usize));
if s.is_empty() {
return $String::new();
}
// SAFETY NOTE: This method produces an F_OWNED ns[C]String from
// a Box<[$char_t]>. this is only safe because in the Gecko
// tree, we use the same allocator for Rust code as for C++