Skip to content

Commit

Permalink
Remove unsafe and platform dependent errno retrieval
Browse files Browse the repository at this point in the history
The recent addition of user ring buffers included calls to `*libc::__errno_location()`.

Apart from requiring an unsafe block, this function might not be available on some targets,
such as Android, causing build failures.

Use the portable implementation provided by `std::io::Error::last_os_error` instead.
  • Loading branch information
amirbou authored and danielocfb committed May 20, 2024
1 parent f367e12 commit 8e6cb2e
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions libbpf-rs/src/user_ringbuf.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use libc::E2BIG;
use libc::ENOSPC;
use std::io;
use std::ops::Deref;
use std::ops::DerefMut;
use std::os::fd::AsFd;
Expand Down Expand Up @@ -89,8 +90,7 @@ impl UserRingBuffer {

let ptr = NonNull::new(raw_ptr).ok_or_else(|| {
// Safely get the last OS error after a failed call to user_ring_buffer__new
let errno = unsafe { *libc::__errno_location() };
Error::from_raw_os_error(errno)
io::Error::last_os_error()
})?;

Ok(UserRingBuffer { ptr })
Expand All @@ -114,11 +114,11 @@ impl UserRingBuffer {

let ptr = NonNull::new(sample_ptr).ok_or_else(|| {
// Fetch the current value of errno to determine the type of error.
let errno = unsafe { *libc::__errno_location() };
match errno {
E2BIG => Error::with_invalid_data("requested size is too large"),
ENOSPC => Error::with_invalid_data("not enough space in the ring buffer"),
_ => Error::from_raw_os_error(errno),
let errno = io::Error::last_os_error();
match errno.raw_os_error() {
Some(E2BIG) => Error::with_invalid_data("requested size is too large"),
Some(ENOSPC) => Error::with_invalid_data("not enough space in the ring buffer"),
_ => Error::from(errno),
}
})?;

Expand Down

0 comments on commit 8e6cb2e

Please sign in to comment.