quinnj / JSON3.jl

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Segmentation faults

lbilli opened this issue · comments

As I'm really excited about this new stab at JSON parsing, I gave it a try right away and I bumped into several segfaults (julia crashing).
As they were unpredictable, this is the closest to a reproducible example I could get.
This works:

using JSON3
txt = """
{ "a" : { "b" : [ 1, 2 ],
          "c" : [ 3, 4 ] } }
"""

jj = JSON3.read(txt)
jj.a.b

This, however, when pasted in a fresh Julia session, most likely gives the wrong result or, more often than not, brings down the session:

using JSON3
txt = """
{ "a" : { "b" : [ 1, 2 ],
          "c" : [ 3, 4 ] } }
"""

JSON3.read(txt).a.b

This happens only the first time the last line is executed in a new session: i.e. subsequent executions work as expected.
Looks like some uninitialized memory access...
I'm on Julia 1.1.1

Thanks for the report @lbilli and trying JSON3.jl out! I have a fix for this here. Turns out my use of unsafe_wrap was indeed unsafe, as illustrated by your example here; the problem is that we created a "tape" to parse the original full object, but, as in the 2nd case, when you only access a child node and there are no references to the original object, our tape gets garbage collected, which in turn invalidates the child tape (since they all share the same tape for efficiency). The solution is to allow our "tape" to be a SubArray which will hold a reference to the original tape, ensuring it doesn't get garbage collected before we need it.