Skip to content

Commit 8e6cb2e

Browse files
amirboudanielocfb
authored andcommitted
Remove unsafe and platform dependent errno retrieval
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.
1 parent f367e12 commit 8e6cb2e

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

libbpf-rs/src/user_ringbuf.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use libc::E2BIG;
22
use libc::ENOSPC;
3+
use std::io;
34
use std::ops::Deref;
45
use std::ops::DerefMut;
56
use std::os::fd::AsFd;
@@ -89,8 +90,7 @@ impl UserRingBuffer {
8990

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

9696
Ok(UserRingBuffer { ptr })
@@ -114,11 +114,11 @@ impl UserRingBuffer {
114114

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

0 commit comments

Comments
 (0)