From 7fa2f10ca78eb5148a810d8a23c974f97a89ba46 Mon Sep 17 00:00:00 2001 From: Micah Snyder Date: Fri, 14 Jun 2024 16:34:12 -0400 Subject: [PATCH] Fix FFI compatility issue when building on macOS When building on an Apple-silicon (ARM) Mac device, the following error occurs: error[E0308]: mismatched types --> /usr/local/cargo/registry/src/index.crates.io-6f17d22bba15001f/clamav-async-0.2.0/src/engine.rs:56:36 | | CStr::from_ptr(c_virname).to_string_lossy().to_string(), | -------------- ^^^^^^^^^ expected `*const u8`, found `*const i8` | | | arguments to this function are incorrect | = note: expected raw pointer `*const u8` found raw pointer `*const i8` note: associated function defined here --> /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/ffi/c_str.rs:274:25 The issue is using 'i8' directly rather than the FFI type which is c_char. --- src/cvd/head_libclamav.rs | 7 +++++-- src/engine.rs | 7 +++++-- src/lib.rs | 6 +++--- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/cvd/head_libclamav.rs b/src/cvd/head_libclamav.rs index b169e19..df6d034 100644 --- a/src/cvd/head_libclamav.rs +++ b/src/cvd/head_libclamav.rs @@ -15,7 +15,10 @@ // MA 02110-1301, USA. use super::{HeadError, Meta}; -use std::{borrow::Cow, ffi::CStr}; +use std::{ + borrow::Cow, + ffi::{c_char, CStr}, +}; /// The header of a CVD pub struct Header(*mut clamav_sys::cl_cvd); @@ -25,7 +28,7 @@ impl Meta for Header { /// (or CLD) file fn from_header_bytes(bytes: &[u8; 512]) -> Result { unsafe { - let raw = clamav_sys::cl_cvdparse(bytes.as_ptr() as *const i8); + let raw = clamav_sys::cl_cvdparse(bytes.as_ptr() as *const c_char); if raw.is_null() { Err(HeadError::Parse) diff --git a/src/engine.rs b/src/engine.rs index 45591c7..33130ff 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -19,7 +19,7 @@ use clamav_sys::cl_engine_field; use clamav_sys::{cl_error_t, time_t}; use core::num; use derivative::Derivative; -use std::ffi::NulError; +use std::ffi::{c_char, NulError}; use std::{path::Path, pin::Pin, sync::Arc, time}; #[cfg(windows)] @@ -45,7 +45,10 @@ pub enum ScanResult { } impl ScanResult { - pub(crate) fn from_ffi(scan_result: cl_error_t, c_virname: *const i8) -> Result { + pub(crate) fn from_ffi( + scan_result: cl_error_t, + c_virname: *const c_char, + ) -> Result { use std::ffi::CStr; match scan_result { diff --git a/src/lib.rs b/src/lib.rs index 848ca28..839f6ca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,7 +39,7 @@ pub use engine::Error as EngineError; pub use error::Error as ClamError; use lazy_static::lazy_static; use std::{ - ffi::CStr, + ffi::{c_char, CStr}, pin::Pin, sync::{Arc, Mutex, Once}, }; @@ -112,8 +112,8 @@ pub fn set_msg_callback(cb: MsgCallback) { /// unsafe extern "C" fn clcb_msg_wrapper( severity: clamav_sys::cl_msg, - fullmsg: *const i8, - msg: *const i8, + fullmsg: *const c_char, + msg: *const c_char, _context: *mut libc::c_void, ) { // Remap the log level to "standard" Rust log levels