Skip to content

Replace finalizerFree with c_pcre_free

Daniel Casanueva requested to merge github/fork/lyokha/master into main

Created by: lyokha

I want to use this library with custom allocator (Nginx pool allocator) which substitutes pcre_malloc and pcre_free function pointers with custom functions. Currently, pcre-light calls finalizerFree unconditionally, which makes my module crash when the compiled regexes get deleted.

This fix provides correct replacement of finalizerFree with the actual finalizer stored in pcre_free. Notice that it uses GHC extension CApiFFI available since GHC 7.10 as the older ccall api is unable to correctly pull the finalizer from a C function pointer (pcre_free).

Notice also that GHC may throw a warning (my GHC version is 8.8.4)

[1 of 3] Compiling Text.Regex.PCRE.Light.Base ( dist/build/Text/Regex/PCRE/Light/Base.hs, dist/build/Text/Regex/PCRE/Light/Base.o )

Text/Regex/PCRE/Light/Base.hsc:830:1: warning: [-Wdodgy-foreign-imports]
    possible missing & in foreign import of FunPtr
    |
830 | foreign import capi "pcre.h value pcre_free"
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^...

This can be safely ignored as pcre_free is not a function, but a function pointer, and GHC thinks by some reason (perhaps historically) that the only type of values that can be pulled from a C code with foreign import is a function.

I also tested that the correct finalizer gets called when the compiled regexes get deleted.

Edited by Daniel Casanueva

Merge request reports