New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can segfault #5

Open
ndmitchell opened this Issue Nov 27, 2016 · 4 comments

Comments

Projects
None yet
2 participants
@ndmitchell
Collaborator

ndmitchell commented Nov 27, 2016

The library can segfault. Pugi requires the document to be kept alive while any nodes are being used, but the Haskell binding doesn't ensure that.

@ndmitchell

This comment has been minimized.

Show comment
Hide comment
@ndmitchell

ndmitchell Nov 27, 2016

Collaborator

Example code:

segfault :: IO ()
segfault = forever $ do
    bs <- BS.readFile "example.xml"
    let Right root = parse def bs
    let unattend = fromJust $ child "unattend" root
    print $ show unattend
    E.evaluate bs
Collaborator

ndmitchell commented Nov 27, 2016

Example code:

segfault :: IO ()
segfault = forever $ do
    bs <- BS.readFile "example.xml"
    let Right root = parse def bs
    let unattend = fromJust $ child "unattend" root
    print $ show unattend
    E.evaluate bs
@dbeacham

This comment has been minimized.

Show comment
Hide comment
@dbeacham

dbeacham Apr 11, 2017

Are there any workarounds to this?

I'd prefer a more permanent fix but couldn't determine at first glance what might need to be done. Are there any hints on what such a fix would look like?

dbeacham commented Apr 11, 2017

Are there any workarounds to this?

I'd prefer a more permanent fix but couldn't determine at first glance what might need to be done. Are there any hints on what such a fix would look like?

@dbeacham

This comment has been minimized.

Show comment
Hide comment
@dbeacham

dbeacham Apr 11, 2017

Being a bit dense above - easy to work around by keeping the doc node in scope until I've finished with it! Annoying but it works for now...

I'll also have a read through of the pugixml docs to see where / how I can set up the FFI finalizers to keep the doc around until after all the nodes have been used. Is anyone aware of any other FFI bindings that do a similar thing?

dbeacham commented Apr 11, 2017

Being a bit dense above - easy to work around by keeping the doc node in scope until I've finished with it! Annoying but it works for now...

I'll also have a read through of the pugixml docs to see where / how I can set up the FFI finalizers to keep the doc around until after all the nodes have been used. Is anyone aware of any other FFI bindings that do a similar thing?

@ndmitchell

This comment has been minimized.

Show comment
Hide comment
@ndmitchell

ndmitchell Apr 12, 2017

Collaborator

Pugixml deliberately doesn't require the nodes to be kept alive - they a single pointers that reference inside the original doc. The real solution is not to keep alive the memory that Pugixml-hs allocated, but to skip the memory allocation in the first place. As for an example of a library that follows that pattern, Hexml is the perfect example. see here, where each node points at the central document.

I was working on a patch to do the above, but then discovered even with those changes the underlying Pugixml was too slow, so went off and developed Hexml.

Collaborator

ndmitchell commented Apr 12, 2017

Pugixml deliberately doesn't require the nodes to be kept alive - they a single pointers that reference inside the original doc. The real solution is not to keep alive the memory that Pugixml-hs allocated, but to skip the memory allocation in the first place. As for an example of a library that follows that pattern, Hexml is the perfect example. see here, where each node points at the central document.

I was working on a patch to do the above, but then discovered even with those changes the underlying Pugixml was too slow, so went off and developed Hexml.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment