Bug 1546048 - Use std::ffi::CStr and the cstr crate instead of custom stuff in xpcom. r=lina

Also, this checks for strings with nulls, which is nice (though I guess an
uncommon mistake).

Differential Revision: https://phabricator.services.mozilla.com/D28312

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Emilio Cobos Álvarez 2019-04-22 05:09:23 +00:00
parent c3f52cb2cd
commit 3bb02fa89f
11 changed files with 18 additions and 42 deletions

4
Cargo.lock generated
View File

@ -1,7 +1,5 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "Inflector"
version = "0.11.2"
@ -358,6 +356,7 @@ name = "bookmark_sync"
version = "0.1.0"
dependencies = [
"atomic_refcell 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"cstr 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"dogear 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
@ -3438,6 +3437,7 @@ name = "xulstore"
version = "0.1.0"
dependencies = [
"crossbeam-utils 0.6.3 (registry+https://github.com/rust-lang/crates.io-index)",
"cstr 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"failure 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -9,6 +9,7 @@ atomic_refcell = "0.1"
dogear = "0.2.3"
libc = "0.2"
log = "0.4"
cstr = "0.1"
moz_task = { path = "../../../../xpcom/rust/moz_task" }
nserror = { path = "../../../../xpcom/rust/nserror" }
nsstring = { path = "../../../../xpcom/rust/nsstring" }

View File

@ -4,6 +4,8 @@
#![allow(non_snake_case)]
#[macro_use]
extern crate cstr;
#[macro_use]
extern crate xpcom;

View File

@ -154,7 +154,7 @@ impl MergeTask {
.unwrap_or(LevelFilter::Off);
let logger = match logger {
Some(logger) => Some(ThreadPtrHolder::new(
c_str!("mozISyncedBookmarksMirrorLogger"),
cstr!("mozISyncedBookmarksMirrorLogger"),
logger,
)?),
None => None,
@ -166,7 +166,7 @@ impl MergeTask {
local_time_millis: local_time_seconds * 1000,
remote_time_millis: remote_time_seconds * 1000,
weak_uploads,
callback: ThreadPtrHolder::new(c_str!("mozISyncedBookmarksMirrorCallback"), callback)?,
callback: ThreadPtrHolder::new(cstr!("mozISyncedBookmarksMirrorCallback"), callback)?,
result: AtomicRefCell::default(),
})
}

View File

@ -6,6 +6,7 @@ license = "MPL-2.0"
[dependencies]
crossbeam-utils = "0.6.3"
cstr = "0.1"
lazy_static = "1.0"
libc = "0.2"
lmdb-rkv = "0.11.2"

View File

@ -4,6 +4,8 @@
extern crate crossbeam_utils;
#[macro_use]
extern crate cstr;
#[macro_use]
extern crate failure;
#[macro_use]
extern crate lazy_static;

View File

@ -69,7 +69,7 @@ fn observe_xpcom_shutdown() {
let observer = XpcomShutdownObserver::new();
unsafe {
obs_svc
.AddObserver(observer.coerce(), c_str!("xpcom-shutdown").as_ptr(), false)
.AddObserver(observer.coerce(), cstr!("xpcom-shutdown").as_ptr(), false)
.to_result()?
};
Ok(())

View File

@ -79,7 +79,7 @@ fn get_profile_dir() -> XULStoreResult<PathBuf> {
unsafe {
dir_svc
.Get(
c_str!("ProfD").as_ptr(),
cstr!("ProfD").as_ptr(),
&nsIFile::IID,
profile_dir.void_ptr(),
)
@ -87,7 +87,7 @@ fn get_profile_dir() -> XULStoreResult<PathBuf> {
.or_else(|_| {
dir_svc
.Get(
c_str!("ProfDS").as_ptr(),
cstr!("ProfDS").as_ptr(),
&nsIFile::IID,
profile_dir.void_ptr(),
)
@ -131,7 +131,7 @@ fn observe_profile_change() {
obs_svc
.AddObserver(
observer.coerce(),
c_str!("profile-after-change").as_ptr(),
cstr!("profile-after-change").as_ptr(),
false,
)
.to_result()?

View File

@ -18,11 +18,12 @@ use std::{
marker::PhantomData,
mem, ptr,
sync::atomic::{AtomicBool, Ordering},
ffi::CStr,
};
use xpcom::{
getter_addrefs,
interfaces::{nsIEventTarget, nsIRunnable, nsISupports, nsIThread},
xpcom, xpcom_method, AtomicRefcnt, NulTerminatedCStr, RefCounted, RefPtr, XpCom,
xpcom, xpcom_method, AtomicRefcnt, RefCounted, RefPtr, XpCom,
};
extern "C" {
@ -141,7 +142,7 @@ pub type ThreadPtrHandle<T> = RefPtr<ThreadPtrHolder<T>>;
pub struct ThreadPtrHolder<T: XpCom + 'static> {
ptr: *const T,
marker: PhantomData<T>,
name: NulTerminatedCStr,
name: &'static CStr,
owning_thread: RefPtr<nsIThread>,
refcnt: AtomicRefcnt,
}
@ -184,7 +185,7 @@ unsafe impl<T: XpCom + 'static> RefCounted for ThreadPtrHolder<T> {
impl<T: XpCom + 'static> ThreadPtrHolder<T> {
/// Creates a new owning thread pointer holder. Returns an error if the
/// thread manager has shut down. Panics if `name` isn't a valid C string.
pub fn new(name: NulTerminatedCStr, ptr: RefPtr<T>) -> Result<RefPtr<Self>, nsresult> {
pub fn new(name: &'static CStr, ptr: RefPtr<T>) -> Result<RefPtr<Self>, nsresult> {
let owning_thread = get_current_thread()?;
// Take ownership of the `RefPtr`. This does _not_ decrement its
// refcount, which is what we want. Once we've released all references

View File

@ -1,28 +0,0 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use libc::c_char;
/// Creates a static C string by adding a nul terminator to `$str`.
#[macro_export]
macro_rules! c_str {
($str:expr) => {
$crate::NulTerminatedCStr(concat!($str, '\0').as_bytes())
};
}
/// A nul-terminated, static C string. This should only be created via the
/// `c_str` macro.
pub struct NulTerminatedCStr(pub &'static [u8]);
impl NulTerminatedCStr {
/// Returns a raw pointer to this string, asserting that it's
/// nul-terminated. This pointer can be passed to any C function expecting a
/// `const char*`, or any XPIDL method expecting an `in string`.
#[inline]
pub fn as_ptr(&self) -> *const c_char {
assert_eq!(self.0.last(), Some(&0), "C strings must be nul-terminated");
self.0 as *const [u8] as *const c_char
}
}

View File

@ -28,9 +28,6 @@ pub use xpcom_macros::*;
mod base;
pub use base::*;
mod cstr;
pub use cstr::*;
// Declarative macro to generate XPCOM method stubs.
mod method;
pub use method::*;