Skip to content

Commit

Permalink
Add a few changes for #996
Browse files Browse the repository at this point in the history
Add a few fixes on top of pull request #996. Among others, remove an
'as' cast which potentially could be the source of unsoundness, remove
some other unnecessary ones, and don't repeat code needlessly.

Signed-off-by: Daniel Müller <deso@posteo.net>
  • Loading branch information
d-e-s-o authored and danielocfb committed Nov 8, 2024
1 parent 010994b commit c3ed425
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
6 changes: 5 additions & 1 deletion libbpf-rs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
Unreleased
----------
- Added `Map::lookup_batch` and `Map::lookup_and_delete_batch` method


0.24.8
------
- Added `Program::attach_netfilter_with_opts` for attaching to netfilter
hooks
- Added `Map::lookup_batch` and `Map::lookup_and_delete_batch` method


0.24.5
Expand Down
38 changes: 17 additions & 21 deletions libbpf-rs/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1397,31 +1397,27 @@ impl<'map> BatchedMapIter<'map> {
}

fn lookup_next_batch(&mut self) {
let prev = self.prev.as_ref().map_or(ptr::null(), |p| p.as_ptr());
let prev = self
.prev
.as_mut()
.map_or(ptr::null_mut(), |p| p.as_mut_ptr());
let mut count = self.count as u32;

let ret = unsafe {
if self.delete {
libbpf_sys::bpf_map_lookup_and_delete_batch(
self.map_fd.as_raw_fd(),
prev as _,
self.next.as_mut_ptr().cast(),
self.keys.as_mut_ptr().cast(),
self.values.as_mut_ptr().cast(),
(&mut count) as *mut u32,
&self.batch_opts as *const libbpf_sys::bpf_map_batch_opts,
)
let lookup_fn = if self.delete {
libbpf_sys::bpf_map_lookup_and_delete_batch
} else {
libbpf_sys::bpf_map_lookup_batch(
self.map_fd.as_raw_fd(),
prev as _,
self.next.as_mut_ptr().cast(),
self.keys.as_mut_ptr().cast(),
self.values.as_mut_ptr().cast(),
(&mut count) as *mut u32,
&self.batch_opts as *const libbpf_sys::bpf_map_batch_opts,
)
}
libbpf_sys::bpf_map_lookup_batch
};
lookup_fn(
self.map_fd.as_raw_fd(),
prev.cast(),
self.next.as_mut_ptr().cast(),
self.keys.as_mut_ptr().cast(),
self.values.as_mut_ptr().cast(),
&mut count,
&self.batch_opts,
)
};

if let Err(e) = util::parse_ret(ret) {
Expand Down

0 comments on commit c3ed425

Please sign in to comment.