From 57cd35172f1534444a548460de6eae4680488711 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Sat, 20 Apr 2024 09:47:31 +0200 Subject: [PATCH] Appease clippy ``` warning: casting `u8` to `u64` may become silently lossy if you later change the type warning: casting `u16` to `u64` may become silently lossy if you later change the type warning: casting `u32` to `u64` may become silently lossy if you later change the type warning: casting `i64` to `u64` may lose the sign of the value warning: casting `i8` to `i64` may become silently lossy if you later change the type warning: casting `i64` to `u64` may lose the sign of the value warning: casting `i16` to `i64` may become silently lossy if you later change the type warning: casting `i32` to `i64` may become silently lossy if you later change the type --> ebpf/aya-ebpf/src/helpers.rs:753:27 | 753 | PrintkArg(x as $via as u64) | ^ ... 759 | / impl_integer_promotion!( 760 | | char: via u64, 761 | | u8: via u64, 762 | | u16: via u64, ... | 770 | | isize: via i64, 771 | | ); | |_- in this macro invocation | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cast_lossless = note: this warning originates in the macro `impl_integer_promotion` (in Nightly builds, run with -Z macro-backtrace for more info) ``` Pass an array of bytes instead of u64 to avoid clippy sign warnings. --- ebpf/aya-ebpf/src/helpers.rs | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/ebpf/aya-ebpf/src/helpers.rs b/ebpf/aya-ebpf/src/helpers.rs index 88d0a812..ad7e0ce5 100644 --- a/ebpf/aya-ebpf/src/helpers.rs +++ b/ebpf/aya-ebpf/src/helpers.rs @@ -734,13 +734,13 @@ pub use bpf_printk; /// Argument ready to be passed to `printk` BPF helper. #[repr(transparent)] #[derive(Copy, Clone)] -pub struct PrintkArg(u64); +pub struct PrintkArg([u8; 8]); impl PrintkArg { /// Manually construct a `printk` BPF helper argument. #[inline] pub fn from_raw(x: u64) -> Self { - Self(x) + Self(x.to_ne_bytes()) } } @@ -750,31 +750,31 @@ macro_rules! impl_integer_promotion { impl From<$ty> for PrintkArg { #[inline] fn from(x: $ty) -> PrintkArg { - PrintkArg(x as $via as u64) + PrintkArg((x as $via).to_ne_bytes()) } } )*} } impl_integer_promotion!( - char: via u64, - u8: via u64, - u16: via u64, - u32: via u64, - u64: via u64, - usize: via u64, - i8: via i64, - i16: via i64, - i32: via i64, - i64: via i64, - isize: via i64, + char: via usize, + u8: via usize, + u16: via usize, + u32: via usize, + u64: via usize, + usize: via usize, + i8: via isize, + i16: via isize, + i32: via isize, + i64: via isize, + isize: via isize, ); /// Construct `printk` BPF helper arguments from constant pointers. impl From<*const T> for PrintkArg { #[inline] fn from(x: *const T) -> Self { - PrintkArg(x as usize as u64) + PrintkArg((x as usize).to_ne_bytes()) } } @@ -782,7 +782,7 @@ impl From<*const T> for PrintkArg { impl From<*mut T> for PrintkArg { #[inline] fn from(x: *mut T) -> Self { - PrintkArg(x as usize as u64) + PrintkArg((x as usize).to_ne_bytes()) } }