Temporary change return value condition to avoid problems with possibly (not aware) cast from int to long

pull/326/head
tirex 2 years ago
parent 5b1a3ed866
commit 42c4d5c3af

@ -48,11 +48,11 @@ pub unsafe fn bpf_probe_read<T>(src: *const T) -> Result<T, c_long> {
mem::size_of::<T>() as u32, mem::size_of::<T>() as u32,
src as *const c_void, src as *const c_void,
); );
if ret < 0 { if ret == 0 {
return Err(ret);
}
Ok(v.assume_init()) Ok(v.assume_init())
} else {
Err(ret)
}
} }
/// Read bytes from the pointer `src` into the provided destination buffer. /// Read bytes from the pointer `src` into the provided destination buffer.
@ -84,11 +84,11 @@ pub unsafe fn bpf_probe_read_buf(src: *const u8, dst: &mut [u8]) -> Result<(), c
dst.len() as u32, dst.len() as u32,
src as *const c_void, src as *const c_void,
); );
if ret < 0 { if ret == 0 {
return Err(ret);
}
Ok(()) Ok(())
} else {
Err(ret)
}
} }
/// Read bytes stored at the _user space_ pointer `src` and store them as a `T`. /// Read bytes stored at the _user space_ pointer `src` and store them as a `T`.
@ -121,11 +121,11 @@ pub unsafe fn bpf_probe_read_user<T>(src: *const T) -> Result<T, c_long> {
mem::size_of::<T>() as u32, mem::size_of::<T>() as u32,
src as *const c_void, src as *const c_void,
); );
if ret < 0 { if ret == 0 {
return Err(ret);
}
Ok(v.assume_init()) Ok(v.assume_init())
} else {
Err(ret)
}
} }
/// Read bytes from the _user space_ pointer `src` into the provided destination /// Read bytes from the _user space_ pointer `src` into the provided destination
@ -155,11 +155,11 @@ pub unsafe fn bpf_probe_read_user_buf(src: *const u8, dst: &mut [u8]) -> Result<
dst.len() as u32, dst.len() as u32,
src as *const c_void, src as *const c_void,
); );
if ret < 0 { if ret == 0 {
return Err(ret);
}
Ok(()) Ok(())
} else {
Err(ret)
}
} }
/// Read bytes stored at the _kernel space_ pointer `src` and store them as a `T`. /// Read bytes stored at the _kernel space_ pointer `src` and store them as a `T`.
@ -192,11 +192,11 @@ pub unsafe fn bpf_probe_read_kernel<T>(src: *const T) -> Result<T, c_long> {
mem::size_of::<T>() as u32, mem::size_of::<T>() as u32,
src as *const c_void, src as *const c_void,
); );
if ret < 0 { if ret == 0 {
return Err(ret);
}
Ok(v.assume_init()) Ok(v.assume_init())
} else {
Err(ret)
}
} }
/// Read bytes from the _kernel space_ pointer `src` into the provided destination /// Read bytes from the _kernel space_ pointer `src` into the provided destination
@ -226,11 +226,11 @@ pub unsafe fn bpf_probe_read_kernel_buf(src: *const u8, dst: &mut [u8]) -> Resul
dst.len() as u32, dst.len() as u32,
src as *const c_void, src as *const c_void,
); );
if ret < 0 { if ret == 0 {
return Err(ret);
}
Ok(()) Ok(())
} else {
Err(ret)
}
} }
/// Read a null-terminated string stored at `src` into `dest`. /// Read a null-terminated string stored at `src` into `dest`.
@ -397,11 +397,11 @@ pub unsafe fn bpf_probe_write_user<T>(dst: *mut T, src: *const T) -> Result<(),
src as *const c_void, src as *const c_void,
mem::size_of::<T>() as u32, mem::size_of::<T>() as u32,
); );
if ret < 0 { if ret == 0 {
return Err(ret);
}
Ok(()) Ok(())
} else {
Err(ret)
}
} }
/// Read the `comm` field associated with the current task struct /// Read the `comm` field associated with the current task struct

@ -348,7 +348,7 @@ fn insert<K, V>(def: *mut bpf_map_def, key: &K, value: &V, flags: u64) -> Result
flags, flags,
) )
}; };
(ret >= 0).then(|| ()).ok_or(ret) (ret == 0).then(|| ()).ok_or(ret)
} }
#[inline] #[inline]

@ -78,7 +78,7 @@ impl<K, V> LpmTrie<K, V> {
flags, flags,
) )
}; };
(ret >= 0).then(|| ()).ok_or(ret) (ret == 0).then(|| ()).ok_or(ret)
} }
#[inline] #[inline]

@ -53,14 +53,14 @@ impl<T> Queue<T> {
flags, flags,
) )
}; };
(ret >= 0).then(|| ()).ok_or(ret) (ret == 0).then(|| ()).ok_or(ret)
} }
pub fn pop(&self) -> Option<T> { pub fn pop(&self) -> Option<T> {
unsafe { unsafe {
let mut value = mem::MaybeUninit::uninit(); let mut value = mem::MaybeUninit::uninit();
let ret = bpf_map_pop_elem(self.def.get() as *mut _, value.as_mut_ptr() as *mut _); let ret = bpf_map_pop_elem(self.def.get() as *mut _, value.as_mut_ptr() as *mut _);
(ret >= 0).then(|| value.assume_init()) (ret == 0).then(|| value.assume_init())
} }
} }
} }

@ -61,7 +61,7 @@ impl<K> SockHash<K> {
flags, flags,
) )
}; };
(ret >= 0).then(|| ()).ok_or(ret) (ret == 0).then(|| ()).ok_or(ret)
} }
pub fn redirect_msg(&self, ctx: &SkMsgContext, key: &mut K, flags: u64) -> i64 { pub fn redirect_msg(&self, ctx: &SkMsgContext, key: &mut K, flags: u64) -> i64 {
@ -102,7 +102,7 @@ impl<K> SockHash<K> {
} }
let ret = bpf_sk_assign(ctx.as_ptr() as *mut _, sk, flags); let ret = bpf_sk_assign(ctx.as_ptr() as *mut _, sk, flags);
bpf_sk_release(sk); bpf_sk_release(sk);
(ret >= 0).then(|| ()).ok_or(1) (ret == 0).then(|| ()).ok_or(1)
} }
} }
} }

@ -61,10 +61,10 @@ impl SockMap {
&mut index as *mut _ as *mut c_void, &mut index as *mut _ as *mut c_void,
flags, flags,
); );
if ret < 0 { if ret == 0 {
Err(ret)
} else {
Ok(()) Ok(())
} else {
Err(ret)
} }
} }
@ -102,7 +102,7 @@ impl SockMap {
} }
let ret = bpf_sk_assign(ctx.as_ptr() as *mut _, sk, flags); let ret = bpf_sk_assign(ctx.as_ptr() as *mut _, sk, flags);
bpf_sk_release(sk); bpf_sk_release(sk);
(ret >= 0).then(|| ()).ok_or(1) (ret == 0).then(|| ()).ok_or(1)
} }
} }
} }

@ -47,10 +47,10 @@ impl StackTrace {
pub unsafe fn get_stackid<C: BpfContext>(&self, ctx: &C, flags: u64) -> Result<i64, i64> { pub unsafe fn get_stackid<C: BpfContext>(&self, ctx: &C, flags: u64) -> Result<i64, i64> {
let ret = bpf_get_stackid(ctx.as_ptr(), self.def.get() as *mut _, flags); let ret = bpf_get_stackid(ctx.as_ptr(), self.def.get() as *mut _, flags);
if ret < 0 { if ret == 0 {
Err(ret) Ok(())
} else { } else {
Ok(ret) Err(ret)
} }
} }
} }

@ -57,11 +57,11 @@ impl SkBuffContext {
&mut data as *mut _ as *mut _, &mut data as *mut _ as *mut _,
mem::size_of::<T>() as u32, mem::size_of::<T>() as u32,
); );
if ret < 0 { if ret == 0 {
return Err(ret);
}
Ok(data.assume_init()) Ok(data.assume_init())
} else {
Err(ret)
}
} }
} }
@ -75,12 +75,12 @@ impl SkBuffContext {
mem::size_of::<T>() as u32, mem::size_of::<T>() as u32,
flags, flags,
); );
if ret < 0 { if ret == 0 {
return Err(ret); Ok(())
} else {
Err(ret)
} }
} }
Ok(())
} }
#[inline] #[inline]
@ -93,12 +93,12 @@ impl SkBuffContext {
) -> Result<(), c_long> { ) -> Result<(), c_long> {
unsafe { unsafe {
let ret = bpf_l3_csum_replace(self.skb as *mut _, offset as u32, from, to, size); let ret = bpf_l3_csum_replace(self.skb as *mut _, offset as u32, from, to, size);
if ret < 0 { if ret == 0 {
return Err(ret); Ok(())
} else {
Err(ret)
} }
} }
Ok(())
} }
#[inline] #[inline]
@ -111,41 +111,41 @@ impl SkBuffContext {
) -> Result<(), c_long> { ) -> Result<(), c_long> {
unsafe { unsafe {
let ret = bpf_l4_csum_replace(self.skb as *mut _, offset as u32, from, to, flags); let ret = bpf_l4_csum_replace(self.skb as *mut _, offset as u32, from, to, flags);
if ret < 0 { if ret == 0 {
return Err(ret); Ok(())
} else {
Err(ret)
} }
} }
Ok(())
} }
#[inline] #[inline]
pub fn adjust_room(&self, len_diff: i32, mode: u32, flags: u64) -> Result<(), c_long> { pub fn adjust_room(&self, len_diff: i32, mode: u32, flags: u64) -> Result<(), c_long> {
let ret = unsafe { bpf_skb_adjust_room(self.as_ptr() as *mut _, len_diff, mode, flags) }; let ret = unsafe { bpf_skb_adjust_room(self.as_ptr() as *mut _, len_diff, mode, flags) };
if ret < 0 { if ret == 0 {
return Err(ret);
}
Ok(()) Ok(())
} else {
Err(ret)
}
} }
#[inline] #[inline]
pub fn clone_redirect(&self, if_index: u32, flags: u64) -> Result<(), c_long> { pub fn clone_redirect(&self, if_index: u32, flags: u64) -> Result<(), c_long> {
let ret = unsafe { bpf_clone_redirect(self.as_ptr() as *mut _, if_index, flags) }; let ret = unsafe { bpf_clone_redirect(self.as_ptr() as *mut _, if_index, flags) };
if ret < 0 { if ret == 0 {
Err(ret)
} else {
Ok(()) Ok(())
} else {
Err(ret)
} }
} }
#[inline] #[inline]
pub fn change_type(&self, ty: u32) -> Result<(), c_long> { pub fn change_type(&self, ty: u32) -> Result<(), c_long> {
let ret = unsafe { bpf_skb_change_type(self.as_ptr() as *mut _, ty) }; let ret = unsafe { bpf_skb_change_type(self.as_ptr() as *mut _, ty) };
if ret < 0 { if ret == 0 {
Err(ret)
} else {
Ok(()) Ok(())
} else {
Err(ret)
} }
} }
} }

@ -27,10 +27,10 @@ impl SockOpsContext {
pub fn set_cb_flags(&self, flags: i32) -> Result<(), i64> { pub fn set_cb_flags(&self, flags: i32) -> Result<(), i64> {
let ret = unsafe { bpf_sock_ops_cb_flags_set(self.ops, flags) }; let ret = unsafe { bpf_sock_ops_cb_flags_set(self.ops, flags) };
if ret < 0 { if ret == 0 {
Err(ret)
} else {
Ok(()) Ok(())
} else {
Err(ret)
} }
} }

Loading…
Cancel
Save