From 70f990e6f86a50625d2b46afdc99c5376eec7537 Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Fri, 30 Aug 2024 01:09:32 -0400 Subject: [PATCH] use safer pointer casts See https://rust-lang.github.io/rust-clippy/master/index.html#/ptr_as_ptr --- src/vcl/backend.rs | 30 +++++++++++++------------ src/vcl/convert.rs | 52 +++++++++++++++++++++++--------------------- src/vcl/ctx.rs | 12 +++++----- src/vcl/http.rs | 8 +++---- src/vcl/processor.rs | 22 +++++++++---------- src/vcl/vpriv.rs | 8 +++---- src/vcl/vsb.rs | 2 +- src/vcl/ws.rs | 8 +++---- src/vsc.rs | 10 ++++----- 9 files changed, 78 insertions(+), 74 deletions(-) diff --git a/src/vcl/backend.rs b/src/vcl/backend.rs index ca0d77f..be61572 100644 --- a/src/vcl/backend.rs +++ b/src/vcl/backend.rs @@ -295,20 +295,20 @@ unsafe extern "C" fn vfp_pull( let vfe = vfep.as_mut().unwrap(); assert_eq!(vfe.magic, varnish_sys::VFP_ENTRY_MAGIC); - let buf = std::slice::from_raw_parts_mut(ptr as *mut u8, *len as usize); + let buf = std::slice::from_raw_parts_mut(ptr.cast::(), *len as usize); if buf.is_empty() { *len = 0; return varnish_sys::vfp_status_VFP_OK; } - let reader = (vfe.priv1 as *mut T).as_mut().unwrap(); + let reader = vfe.priv1.cast::().as_mut().unwrap(); match reader.read(buf) { Err(e) => { let msg = e.to_string(); // TODO: we should grow a VSL object let t = varnish_sys::txt { - b: msg.as_ptr() as *const c_char, - e: msg.as_ptr().add(msg.len()) as *const c_char, + b: msg.as_ptr().cast::(), + e: msg.as_ptr().add(msg.len()).cast::(), }; varnish_sys::VSLbt((*(*ctxp).req).vsl, varnish_sys::VSL_tag_e_SLT_Error, t); @@ -344,7 +344,7 @@ unsafe extern "C" fn wrap_list, T: Transfer>( detailed: i32, json: i32, ) { - let mut ctx = Ctx::new(ctxp as *mut varnish_sys::vrt_ctx); + let mut ctx = Ctx::new(ctxp.cast_mut()); let mut vsb = Vsb::new(vsbp); assert!(!be.is_null()); assert_eq!((*be).magic, varnish_sys::DIRECTOR_MAGIC); @@ -372,7 +372,7 @@ unsafe extern "C" fn wrap_pipe, T: Transfer>( ctxp: *const varnish_sys::vrt_ctx, be: VCLBackendPtr, ) -> varnish_sys::stream_close_t { - let mut ctx = Ctx::new(ctxp as *mut varnish_sys::vrt_ctx); + let mut ctx = Ctx::new(ctxp.cast_mut()); assert!(!(*ctxp).req.is_null()); assert_eq!((*(*ctxp).req).magic, varnish_sys::REQ_MAGIC); assert!(!(*(*ctxp).req).sp.is_null()); @@ -393,7 +393,7 @@ unsafe extern "C" fn wrap_gethdrs, T: Transfer>( ctxp: *const varnish_sys::vrt_ctx, be: VCLBackendPtr, ) -> ::std::os::raw::c_int { - let mut ctx = Ctx::new(ctxp as *mut varnish_sys::vrt_ctx); + let mut ctx = Ctx::new(ctxp.cast_mut()); assert!(!be.is_null()); assert_eq!((*be).magic, varnish_sys::DIRECTOR_MAGIC); assert!(!(*be).vcl_name.is_null()); @@ -418,8 +418,9 @@ unsafe extern "C" fn wrap_gethdrs, T: Transfer>( let htc = varnish_sys::WS_Alloc( (*ctx.raw.bo).ws.as_mut_ptr(), - std::mem::size_of::() as u32, - ) as *mut varnish_sys::http_conn; + size_of::() as u32, + ) + .cast::(); if htc.is_null() { ctx.fail(&format!("{}: insuficient workspace", (*backend).get_type())); return -1; @@ -445,13 +446,14 @@ unsafe extern "C" fn wrap_gethdrs, T: Transfer>( (*htc).content_length = l as isize; } }; - (*htc).priv_ = Box::into_raw(Box::new(transfer)) as *mut std::ffi::c_void; + (*htc).priv_ = Box::into_raw(Box::new(transfer)).cast::(); // build a vfp to wrap the Transfer object if there's something to push if (*htc).body_status != varnish_sys::BS_NONE.as_ptr() { let vfp = varnish_sys::WS_Alloc( (*ctx.raw.bo).ws.as_mut_ptr(), - std::mem::size_of::() as u32, - ) as *mut varnish_sys::vfp; + size_of::() as u32, + ) + .cast::(); if vfp.is_null() { ctx.fail(&format!("{}: insuficient workspace", (*backend).get_type())); return -1; @@ -468,7 +470,7 @@ unsafe extern "C" fn wrap_gethdrs, T: Transfer>( } Ok(s) => s, }; - (*vfp).name = t.as_ptr() as *const c_char; + (*vfp).name = t.as_ptr().cast::(); (*vfp).init = None; (*vfp).pull = Some(vfp_pull::); (*vfp).fini = None; @@ -562,7 +564,7 @@ unsafe extern "C" fn wrap_finish, T: Transfer>( // drop the Transfer let htc = (*(*ctxp).bo).htc; if !(*htc).priv_.is_null() { - drop(Box::from_raw((*htc).priv_ as *mut T)); + drop(Box::from_raw((*htc).priv_.cast::())); } (*(*ctxp).bo).htc = ptr::null_mut(); diff --git a/src/vcl/convert.rs b/src/vcl/convert.rs index e59e5a2..60435dc 100644 --- a/src/vcl/convert.rs +++ b/src/vcl/convert.rs @@ -142,16 +142,16 @@ impl IntoVCL for &[u8] { // try to save some work if the buffer is already in the workspace // and if it's followed by a null byten if unsafe { - varnish_sys::WS_Allocated( + WS_Allocated( ws.raw, - self.as_ptr() as *const c_void, + self.as_ptr().cast::(), self.len() as isize + 1, ) == 1 && *self.as_ptr().add(self.len()) == b'\0' } { - Ok(self.as_ptr() as *const c_char) + Ok(self.as_ptr().cast::()) } else { - Ok(ws.copy_bytes_with_null(&self)?.as_ptr() as *const c_char) + Ok(ws.copy_bytes_with_null(&self)?.as_ptr().cast::()) } } } @@ -186,8 +186,9 @@ impl + AsRef<[u8]>> IntoVCL for Option { impl<'a> IntoVCL for COWProbe<'a> { fn into_vcl(self, ws: &mut WS) -> Result { let p = ws - .alloc(std::mem::size_of::())? - .as_mut_ptr() as *mut vrt_backend_probe; + .alloc(size_of::())? + .as_mut_ptr() + .cast::(); let probe = unsafe { p.as_mut().unwrap() }; probe.magic = varnish_sys::VRT_BACKEND_PROBE_MAGIC; match self.request { @@ -210,8 +211,9 @@ impl<'a> IntoVCL for COWProbe<'a> { impl IntoVCL for Probe { fn into_vcl(self, ws: &mut WS) -> Result { let p = ws - .alloc(std::mem::size_of::())? - .as_mut_ptr() as *mut vrt_backend_probe; + .alloc(size_of::())? + .as_mut_ptr() + .cast::(); let probe = unsafe { p.as_mut().unwrap() }; probe.magic = varnish_sys::VRT_BACKEND_PROBE_MAGIC; match self.request { @@ -235,26 +237,26 @@ impl IntoVCL for SocketAddr { fn into_vcl(self, ws: &mut WS) -> Result { unsafe { let p = - ws.alloc(varnish_sys::vsa_suckaddr_len)?.as_mut_ptr() as *mut varnish_sys::suckaddr; + ws.alloc(vsa_suckaddr_len)?.as_mut_ptr().cast::(); match self { - std::net::SocketAddr::V4(sa) => { - assert!(!varnish_sys::VSA_BuildFAP( - p as *mut std::ffi::c_void, - varnish_sys::PF_INET as varnish_sys::sa_family_t, - sa.ip().octets().as_slice().as_ptr() as *const std::os::raw::c_void, + SocketAddr::V4(sa) => { + assert!(!VSA_BuildFAP( + p.cast::(), + PF_INET as sa_family_t, + sa.ip().octets().as_slice().as_ptr().cast::(), 4, - &sa.port().to_be() as *const u16 as *const std::os::raw::c_void, + (&sa.port().to_be() as *const u16).cast::(), 2 ) .is_null()); } - std::net::SocketAddr::V6(sa) => { - assert!(!varnish_sys::VSA_BuildFAP( - p as *mut std::ffi::c_void, - varnish_sys::PF_INET6 as varnish_sys::sa_family_t, - sa.ip().octets().as_slice().as_ptr() as *const std::os::raw::c_void, + SocketAddr::V6(sa) => { + assert!(!VSA_BuildFAP( + p.cast::(), + PF_INET6 as sa_family_t, + sa.ip().octets().as_slice().as_ptr().cast::(), 16, - &sa.port().to_be() as *const u16 as *const std::os::raw::c_void, + (&sa.port().to_be() as *const u16).cast::(), 2 ) .is_null()); @@ -515,14 +517,14 @@ impl IntoRust> for VCL_IP { let port = VSA_Port(self) as u16; match fam { - varnish_sys::PF_INET => { - let buf: &[u8; 4] = std::slice::from_raw_parts(ptr as *const u8, 4) + PF_INET => { + let buf: &[u8; 4] = std::slice::from_raw_parts(ptr.cast::(), 4) .try_into() .unwrap(); Some(SocketAddr::new(IpAddr::V4(Ipv4Addr::from(*buf)), port)) } - varnish_sys::PF_INET6 => { - let buf: &[u8; 16] = std::slice::from_raw_parts(ptr as *const u8, 16) + PF_INET6 => { + let buf: &[u8; 16] = std::slice::from_raw_parts(ptr.cast::(), 16) .try_into() .unwrap(); Some(SocketAddr::new(IpAddr::V6(Ipv6Addr::from(*buf)), port)) diff --git a/src/vcl/ctx.rs b/src/vcl/ctx.rs index 9f51aae..d6e8bca 100644 --- a/src/vcl/ctx.rs +++ b/src/vcl/ctx.rs @@ -119,8 +119,8 @@ impl<'a> Ctx<'a> { log(logtag, msg); } else { let t = varnish_sys::txt { - b: msg.as_ptr() as *const c_char, - e: msg.as_ptr().add(msg.len()) as *const c_char, + b: msg.as_ptr().cast::(), + e: msg.as_ptr().add(msg.len()).cast::(), }; varnish_sys::VSLbt(p.vsl, logtag.into_u32(), t); } @@ -134,8 +134,8 @@ impl<'a> Ctx<'a> { ptr: *const c_void, len: isize, ) -> std::os::raw::c_int { - let v = (priv_ as *mut Vec<&'a [u8]>).as_mut().unwrap(); - let buf = std::slice::from_raw_parts(ptr as *const u8, len as usize); + let v = priv_.cast::>().as_mut().unwrap(); + let buf = std::slice::from_raw_parts(ptr.cast::(), len as usize); v.push(buf); 0 } @@ -154,7 +154,7 @@ impl<'a> Ctx<'a> { req.vsl.as_mut_ptr(), req, Some(chunk_collector), - p as *mut c_void, + p.cast::(), ) } { 0 => Ok(*v), @@ -246,7 +246,7 @@ pub fn log(logtag: LogTag, msg: &str) { logtag.into_u32(), varnish_sys::vxids { vxid: 0 }, c"%s".as_ptr(), - c_cstring.as_ptr() as *const u8, + c_cstring.as_ptr().cast::(), ); } } diff --git a/src/vcl/http.rs b/src/vcl/http.rs index 862db1d..f942f97 100644 --- a/src/vcl/http.rs +++ b/src/vcl/http.rs @@ -54,9 +54,9 @@ impl<'a> HTTP<'a> { let hdr_buf = ws.copy_bytes_with_null(&value)?; unsafe { let hd = self.raw.hd.offset(idx as isize); - (*hd).b = hdr_buf.as_ptr() as *const c_char; + (*hd).b = hdr_buf.as_ptr().cast::(); /* -1 accounts for the null character */ - (*hd).e = hdr_buf.as_ptr().add(hdr_buf.len() - 1) as *const c_char; + (*hd).e = hdr_buf.as_ptr().add(hdr_buf.len() - 1).cast::(); let hdf = self.raw.hdf.offset(idx as isize); *hdf = 0; } @@ -136,7 +136,7 @@ impl<'a> HTTP<'a> { None } else { let e = (*self.raw.hd.offset(idx as isize)).e; - let buf = from_raw_parts(b as *const u8, e.offset_from(b) as usize); + let buf = from_raw_parts(b.cast::(), e.offset_from(b) as usize); Some(from_utf8(buf).unwrap()) } } @@ -262,7 +262,7 @@ fn header_from_hd<'a>(txt: *const varnish_sys::txt) -> Option<(&'a str, &'a str) return None; } let e = (*txt).e; - let buf = from_raw_parts(b as *const u8, e.offset_from(b) as usize); + let buf = from_raw_parts(b.cast::(), e.offset_from(b) as usize); let colon = buf.iter().position(|x| *x == b':').unwrap(); diff --git a/src/vcl/processor.rs b/src/vcl/processor.rs index 22f6bdb..cc421e9 100644 --- a/src/vcl/processor.rs +++ b/src/vcl/processor.rs @@ -88,7 +88,7 @@ pub unsafe extern "C" fn gen_vdp_init( oc, ) { InitResult::Ok(proc) => { - *priv_ = Box::into_raw(Box::new(proc)) as *mut c_void; + *priv_ = Box::into_raw(Box::new(proc)).cast::(); 0 } InitResult::Err(_) => -1, // TODO: log error @@ -104,7 +104,7 @@ pub unsafe extern "C" fn gen_vdp_fini( return 0; } assert_ne!(*priv_, ptr::null_mut()); - drop(Box::from_raw(*priv_ as *mut T)); + drop(Box::from_raw((*priv_).cast::())); *priv_ = ptr::null_mut(); 0 } @@ -124,8 +124,8 @@ pub unsafe extern "C" fn gen_vdp_push( varnish_sys::vdp_action_VDP_END => PushAction::End, _ => return 1, /* TODO: log */ }; - let buf = std::slice::from_raw_parts(ptr as *const u8, len as usize); - match (*(*priv_ as *mut T)).push(&mut VDPCtx::new(ctx_raw), out_action, buf) { + let buf = std::slice::from_raw_parts(ptr.cast::(), len as usize); + match (*(*priv_).cast::()).push(&mut VDPCtx::new(ctx_raw), out_action, buf) { PushResult::Err => -1, // TODO: log error PushResult::Ok => 0, PushResult::End => 1, @@ -135,7 +135,7 @@ pub unsafe extern "C" fn gen_vdp_push( /// Create a `varnish_sys::vdp` that can be fed to `varnish_sys::VRT_AddVDP` pub fn new_vdp() -> varnish_sys::vdp { varnish_sys::vdp { - name: T::name().as_ptr() as *const c_char, + name: T::name().as_ptr().cast::(), init: Some(gen_vdp_init::), bytes: Some(gen_vdp_push::), fini: Some(gen_vdp_fini::), @@ -166,7 +166,7 @@ impl<'a> VDPCtx<'a> { varnish_sys::VDP_bytes( self.raw, act as std::os::raw::c_uint, - buf.as_ptr() as *const c_void, + buf.as_ptr().cast::(), buf.len() as isize, ) } { @@ -213,7 +213,7 @@ unsafe extern "C" fn wrap_vfp_init( &mut VFPCtx::new(ctx), ) { InitResult::Ok(proc) => { - vfe.priv1 = Box::into_raw(Box::new(proc)) as *mut c_void; + vfe.priv1 = Box::into_raw(Box::new(proc)).cast::(); 0 } InitResult::Err(_) => -1, // TODO: log the error, @@ -232,8 +232,8 @@ pub unsafe extern "C" fn wrap_vfp_pull( let vfe = vfep.as_mut().unwrap(); assert_eq!(vfe.magic, varnish_sys::VFP_ENTRY_MAGIC); - let buf = std::slice::from_raw_parts_mut(ptr as *mut u8, *len as usize); - let obj = (vfe.priv1 as *mut T).as_mut().unwrap(); + let buf = std::slice::from_raw_parts_mut(ptr.cast::(), *len as usize); + let obj = vfe.priv1.cast::().as_mut().unwrap(); match obj.pull(&mut VFPCtx::new(ctx), buf) { PullResult::Err => varnish_sys::vfp_status_VFP_ERROR, // TODO: log error PullResult::Ok(l) => { @@ -257,14 +257,14 @@ pub unsafe extern "C" fn wrap_vfp_fini(ctxp: *mut vfp_ctx, vfep: *mut vf return; } - drop(Box::from_raw(vfe.priv1 as *mut T)); + drop(Box::from_raw(vfe.priv1.cast::())); vfe.priv1 = ptr::null_mut(); } /// Create a `varnish_sys::vfp` that can be fed to `varnish_sys::VRT_AddVFP` pub fn new_vfp() -> varnish_sys::vfp { varnish_sys::vfp { - name: T::name().as_ptr() as *const c_char, + name: T::name().as_ptr().cast::(), init: Some(wrap_vfp_init::), pull: Some(wrap_vfp_pull::), fini: Some(wrap_vfp_fini::), diff --git a/src/vcl/vpriv.rs b/src/vcl/vpriv.rs index 6cab3d1..daa7cee 100644 --- a/src/vcl/vpriv.rs +++ b/src/vcl/vpriv.rs @@ -36,7 +36,7 @@ impl VPriv { } fn get_inner(&mut self) -> Option<&mut InnerVPriv> { - unsafe { (self.ptr.as_mut()?.priv_ as *mut InnerVPriv).as_mut() } + unsafe { self.ptr.as_mut()?.priv_.cast::>().as_mut() } } pub fn store(&mut self, obj: T) { @@ -56,7 +56,7 @@ impl VPriv { obj: None, }; (*self.ptr).methods = methods_ptr; - (*self.ptr).priv_ = Box::into_raw(Box::new(inner_priv)) as *mut c_void; + (*self.ptr).priv_ = Box::into_raw(Box::new(inner_priv)).cast::(); } } let inner_priv = self.get_inner().unwrap(); @@ -64,7 +64,7 @@ impl VPriv { } pub fn as_ref(&self) -> Option<&T> { - let inner = unsafe { ((*self.ptr).priv_ as *mut InnerVPriv).as_ref()? }; + let inner = unsafe { (*self.ptr).priv_.cast::>().as_ref()? }; inner.obj.as_ref() } @@ -85,7 +85,7 @@ impl VPriv { } unsafe extern "C" fn vpriv_free(_: *const varnish_sys::vrt_ctx, ptr: *mut c_void) { - let inner_priv = Box::from_raw(ptr as *mut InnerVPriv); + let inner_priv = Box::from_raw(ptr.cast::>()); drop(Box::from_raw(inner_priv.name)); drop(Box::from_raw(inner_priv.methods)); } diff --git a/src/vcl/vsb.rs b/src/vcl/vsb.rs index 3ffeed9..e74e83d 100644 --- a/src/vcl/vsb.rs +++ b/src/vcl/vsb.rs @@ -15,7 +15,7 @@ impl<'a> Vsb<'a> { /// Push a buffer into the `Vsb` pub fn cat>(&mut self, src: &T) -> Result<(), ()> { - let buf = src.as_ref().as_ptr() as *const std::ffi::c_void; + let buf = src.as_ref().as_ptr().cast::(); let l = src.as_ref().len(); match unsafe { varnish_sys::VSB_bcat(self.raw, buf, l as isize) } { diff --git a/src/vcl/ws.rs b/src/vcl/ws.rs index cecd465..379c2ff 100644 --- a/src/vcl/ws.rs +++ b/src/vcl/ws.rs @@ -45,7 +45,7 @@ impl<'a> WS<'a> { let wsp = unsafe { self.raw.as_mut().unwrap() }; assert_eq!(wsp.magic, varnish_sys::WS_MAGIC); - let p = unsafe { varnish_sys::WS_Alloc(wsp, sz as u32) as *mut u8 }; + let p = unsafe { varnish_sys::WS_Alloc(wsp, sz as u32).cast::() }; if p.is_null() { Err(format!("workspace allocation ({} bytes) failed", sz)) } else { @@ -67,7 +67,7 @@ impl<'a> WS<'a> { aligned_sz, sz, wsp.f, wsp.e )) } else { - let buf = from_raw_parts_mut(wsp.f as *mut u8, aligned_sz); + let buf = from_raw_parts_mut(wsp.f.cast::(), aligned_sz); wsp.f = wsp.f.add(aligned_sz); Ok(buf) } @@ -119,11 +119,11 @@ impl<'a> WS<'a> { unsafe { let sz = varnish_sys::WS_ReserveAll(wsp) as usize; - let buf = from_raw_parts_mut(wsp.f as *mut u8, sz); + let buf = from_raw_parts_mut(wsp.f.cast::(), sz); ReservedBuf { buf, wsp: self.raw, - b: wsp.f as *mut u8, + b: wsp.f.cast::(), len: 0, } } diff --git a/src/vsc.rs b/src/vsc.rs index dfe326b..6573854 100644 --- a/src/vsc.rs +++ b/src/vsc.rs @@ -98,7 +98,7 @@ impl<'a> VSCBuilder<'a> { let ret = varnish_sys::VSM_Arg( self.vsm, 't' as core::ffi::c_char, - arg.as_ptr() as *const core::ffi::c_char, + arg.as_ptr().cast::(), ); assert!(ret == 1); } @@ -111,7 +111,7 @@ impl<'a> VSCBuilder<'a> { let ret = varnish_sys::VSC_Arg( self.vsc, o as core::ffi::c_char, - c_s.as_ptr() as *const core::ffi::c_char, + c_s.as_ptr().cast::(), ); assert!(ret == 1); } @@ -154,7 +154,7 @@ impl<'a> VSCBuilder<'a> { self.vsc, Some(add_point), Some(del_point), - &mut *internal as *mut VSCInternal as *mut std::ffi::c_void, + (&mut *internal as *mut VSCInternal).cast::(), ); } let vsm = self.vsm; @@ -280,7 +280,7 @@ unsafe extern "C" fn add_point( ptr: *mut std::ffi::c_void, point: *const varnish_sys::VSC_point, ) -> *mut std::ffi::c_void { - let internal = ptr as *mut VSCInternal; + let internal = ptr.cast::(); let k = point as usize; let stat = Stat { @@ -297,7 +297,7 @@ unsafe extern "C" fn add_point( } unsafe extern "C" fn del_point(ptr: *mut std::ffi::c_void, point: *const varnish_sys::VSC_point) { - let internal = ptr as *mut VSCInternal; + let internal = ptr.cast::(); let k = point as usize; assert!((*internal).points.contains_key(&k));