mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-24 13:21:05 +00:00
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:
parent
8681dbd17f
commit
30bd5679fd
@ -129,3 +129,8 @@ extern "C" void Rust_StringWrite();
|
||||
TEST(RustNsString, StringWrite) {
|
||||
Rust_StringWrite();
|
||||
}
|
||||
|
||||
extern "C" void Rust_FromEmptyRustString();
|
||||
TEST(RustNsString, FromEmptyRustString) {
|
||||
Rust_FromEmptyRustString();
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
|
@ -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++
|
||||
|
Loading…
Reference in New Issue
Block a user