JuliaIO / EzXML.jl

XML/HTML handling tools for primates

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Segfault caused by inserting adjascent text nodes

jmert opened this issue · comments

The following reliably causes segfaults for me on both Julia v1.4.1 and Julia master:

using EzXML

for _ in 1:2
    doc = XMLDocument()
    root = ElementNode("root")
    setroot!(doc, root)

    t1 = TextNode("#1")
    link!(root, t1)

    t2 = TextNode("#2")
    linkprev!(t1, t2)
end

The crash occurs in the second linkprev!(t1, t2) call, and the loop is only required to reliably generate the crash. (Without it, typing each line interactively would crash, but sometimes running as a script would be fine.)

I'm inferring that this is a bad interaction between the finalizer and the fact that adjascent text nodes are combined, with the latter new node being freed: http://www.xmlsoft.org/html/libxml-tree.html#xmlAddPrevSibling (or via the actual implementation, https://github.com/GNOME/libxml2/blob/20c60886e42c942e6c45a3a99d583e4d3269a1c1/tree.c#L3075).


More details:

(myproject) pkg> st
Status `~/myproject/Project.toml`
  [8f5d6c58] EzXML v1.1.0
  [0f8b85d8] JSON3 v1.0.2

(myproject) pkg> versioninfo()
ERROR: Could not determine command

julia> versioninfo()
Julia Version 1.5.0-DEV.652
Commit 8d8f46db37* (2020-04-20 18:34 UTC)
DEBUG build
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: AMD Ryzen 3 2200G with Radeon Vega Graphics
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-9.0.1 (ORCJIT, znver1)

Hello,

It seems I have a similar issue, as seen in this MWE:

using EzXML

document = parsehtml("""<p>This <em>is</em> a text.</p>""").root
text = findfirst("//p", document)
parent_node = ElementNode("text")
for child_node  nodes(text)
    # if istext(child_node)
    #     link!(parent_node, TextNode(child_node.content))  # Crashes.
    # end
    link!(parent_node, TextNode("test"))  # Crashes too.
end
link!(parent_node, TextNode("test"))  # Does not crash.

@jmert, did you find a workaround?