messense / mupdf-rs

Rust binding to mupdf
GNU Affero General Public License v3.0
104 stars 23 forks source link

Segmentation fault in image_filter_callback/set_image_filter #62

Closed Shadow53 closed 1 year ago

Shadow53 commented 1 year ago

When I process images using set_image_filter, I get a segmentation fault.

I was able to trace the issue to this line, where ret.inner is copied (since pointers are Copy) and returned, and ret is dropped, which frees the memory using mupdf's fz_drop_image.

Shadow53 commented 1 year ago

The immediate solution I see is to swap ret.inner with a nullptr and return the swapped-out value, but I am wondering if you would be open to a PR with a larger refactor that wraps pointers in Rc/Arc internally to avoid this sort of problem in the future.

messense commented 1 year ago

Sure! Pull requests are welcome.

Shadow53 commented 1 year ago

Worked at this for a bit, only to realize that fz_keep_* and fz_drop_* increase/decrease existing reference counts, so adding Rust-level reference counting would be unnecessary complexity on top of that.

About to open a PR that just fixes this issue instead of refactoring everything.