rust-pcap / pcap

Rust language pcap library
Apache License 2.0
595 stars 138 forks source link

Add Handle that own pointer and call C fn to free a pointer #260

Closed Stargateur closed 4 months ago

Stargateur commented 1 year ago

That an idea to avoid fall in trap of Drop impl like in https://github.com/rust-pcap/pcap/pull/244.

The purpose is to limit at maximum the risk of double free, with this Handle that own pointer it's very unlucky that a double free happen.

There is still one Drop impl of BpfProgram but this is a little more difficult since the free function ask for a struct not a pointer, this struct use a lot of unsafe, see https://github.com/rust-pcap/pcap/issues/261, if we are sure we can malloc to make the clone so we can use free yourself, meaning we could use Handle::new(ptr, free);.

With this PR https://github.com/rust-pcap/pcap/pull/244 should not introduce UB.

Wojtek242 commented 4 months ago

Due to significant changes on main since this was proposed, I will close it. If you get around to addressing the comments and adapting to the current code base, I'll be happy to review it.