vlaci / pyperscan

https://vlaci.github.io/pyperscan
Apache License 2.0
14 stars 1 forks source link

Memory leak in `hyperscan::wrapper::Database::try_clone` #6

Closed bradlarsen closed 1 year ago

bradlarsen commented 1 year ago

Hi there!

I'm adapting the Rust bindings to Hyperscan from this project in my Nosey Parker secrets detector. When doing that, I noticed a memory leak in the Database::try_clone function:

https://github.com/vlaci/pyperscan/blob/4bc92387dd2996cb67fdfbe185ebc172b36f4cbd/src/hyperscan/wrapper.rs#L108-L120

Here, buf is allocated within the Hyperscan library's hs_serialize_database function, and it's the responsibility of the caller to free it appropriately. But in that Rust code, it is simply leaked. It needs to passed to free() after the call to hs_deserialize_database. (The story could be more complicated still, if pyperscan allowed overriding the allocater that Hyperscan uses, but that's not exposed, so I think it's safe to assume that malloc() was used internally for the allocation)