Skip to content

Commit

Permalink
Avoid using generated va_list type
Browse files Browse the repository at this point in the history
  • Loading branch information
EHfive committed Mar 26, 2024
1 parent 44292ae commit 3393219
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions libbpf-rs/src/print.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::ffi::c_void;
use std::io;
use std::io::Write;
use std::mem;
Expand Down Expand Up @@ -54,7 +55,7 @@ static PRINT_CB: LazyLock<Mutex<Option<(PrintLevel, PrintCallback)>>> =
extern "C" fn outer_print_cb(
level: libbpf_sys::libbpf_print_level,
fmtstr: *const c_char,
va_list: *mut libbpf_sys::__va_list_tag,
va_list: *mut c_void,
) -> i32 {
let level = level.into();
if let Some((min_level, func)) = { *PRINT_CB.lock().unwrap() } {
Expand Down Expand Up @@ -115,7 +116,11 @@ extern "C" fn outer_print_cb(
pub fn set_print(
mut callback: Option<(PrintLevel, PrintCallback)>,
) -> Option<(PrintLevel, PrintCallback)> {
let real_cb: libbpf_sys::libbpf_print_fn_t = callback.as_ref().and(Some(outer_print_cb));
// # Safety
// outer_print_cb has the same function signature as libbpf_print_fn_t
let real_cb: libbpf_sys::libbpf_print_fn_t =
unsafe { Some(mem::transmute(outer_print_cb as *const ())) };
let real_cb: libbpf_sys::libbpf_print_fn_t = callback.as_ref().and(real_cb);
mem::swap(&mut callback, &mut *PRINT_CB.lock().unwrap());
unsafe { libbpf_sys::libbpf_set_print(real_cb) };
callback
Expand Down

0 comments on commit 3393219

Please sign in to comment.