Skip to content

Commit

Permalink
Gracefully handle NULL objects from protocol races.
Browse files Browse the repository at this point in the history
  • Loading branch information
elinorbgr authored and vberger committed Apr 20, 2018
1 parent e846a1d commit 7e87e65
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
15 changes: 15 additions & 0 deletions wayland-client/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,24 @@ impl<I: Interface> Proxy<I> {
/// method will always return `false` and you are responsible of not using
/// an object past its destruction (as this would cause a protocol error).
/// You will also be unable to associate any user data pointer to this object.
///
/// In order to handle protocol races, invoking it with a NULL pointer will
/// create an already-dead object.
pub unsafe fn from_c_ptr(ptr: *mut wl_proxy) -> Self {
use wayland_sys::client::*;

if ptr.is_null() {
return Proxy {
_i: ::std::marker::PhantomData,
internal: Some(Arc::new(ProxyInternal {
alive: AtomicBool::new(false),
user_data: AtomicPtr::new(::std::ptr::null_mut()),
})),
ptr: ptr,
is_wrapper: false,
};
}

let is_managed = {
ffi_dispatch!(WAYLAND_CLIENT_HANDLE, wl_proxy_get_listener, ptr)
== &::wayland_sys::RUST_MANAGED as *const u8 as *const _
Expand Down
14 changes: 14 additions & 0 deletions wayland-server/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,21 @@ impl<I: Interface> Resource<I> {
/// method will always return `false` and you are responsible of not using
/// an object past its destruction (as this would cause a protocol error).
/// You will also be unable to associate any user data pointer to this object.
///
/// In order to handle protocol races, invoking it with a NULL pointer will
/// create an already-dead object.
pub unsafe fn from_c_ptr(ptr: *mut wl_resource) -> Self {
if ptr.is_null() {
return Resource {
_i: ::std::marker::PhantomData,
internal: Some(Arc::new(ResourceInternal {
alive: AtomicBool::new(false),
user_data: AtomicPtr::new(::std::ptr::null_mut()),
})),
ptr: ptr,
};
}

let is_managed = {
ffi_dispatch!(
WAYLAND_SERVER_HANDLE,
Expand Down

0 comments on commit 7e87e65

Please sign in to comment.