rust-pcap / pcap

Rust language pcap library
Apache License 2.0
620 stars 142 forks source link

Add a into_state private method and remove 2 transmute #244

Closed Stargateur closed 2 years ago

Stargateur commented 2 years ago

This simply remove 2 unnecessary transmute.

Wojtek242 commented 2 years ago

Merged this manually due to merge conflicts

Wojtek242 commented 2 years ago

FYI, I had to revert this change as it was causing segmentation faults (see #254). However, if there is non-segmentation faulting way to implement this, it would be very welcome.

Stargateur commented 2 years ago

I don't understand how that possible, I think this change reveal an UB somewhere else in the code.

Stargateur commented 2 years ago

The problem is the Drop implementation:

impl<T: State + ?Sized> Drop for Capture<T> {
    fn drop(&mut self) {
        unsafe { raw::pcap_close(self.handle.as_ptr()) }
    }
}
Stargateur commented 2 years ago

We should encapsulate handle: NonNull<raw::pcap_t>, into a type that have drop implement to avoid this trap it in future.

Wojtek242 commented 2 years ago

If you would like to submit a PR with it, it would be welcome