Skip to content
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

Trying to parsefile an empty file triggers mmap exception #98

Closed
IainNZ opened this issue Mar 13, 2015 · 6 comments
Closed

Trying to parsefile an empty file triggers mmap exception #98

IainNZ opened this issue Mar 13, 2015 · 6 comments

Comments

@IainNZ
Copy link
Contributor

IainNZ commented Mar 13, 2015

shell> touch empty.json

julia> import JSON

julia> JSON.parsefile("empty.json")
ERROR: memory mapping failed: Invalid argument
 in systemerror at /usr/local/Cellar/julia/0.3.6_1/lib/julia/sys.dylib
 in mmap at mmap.jl:35
 in mmap_array at mmap.jl:110
 in mmap_array at mmap.jl:4
 in anonymous at /Users/idunning/.julia/v0.3/JSON/src/JSON.jl:280
 in open at /usr/local/Cellar/julia/0.3.6_1/lib/julia/sys.dylib
 in parsefile at /Users/idunning/.julia/v0.3/JSON/src/JSON.jl:279
@stevengj
Copy link
Member

It looks like this may be a bug (or unavoidable limitation? but it seems avoidable) in mmap_array. After doing touch foo to create an empty file, mmap_array(Int, (0,), open("foo", "r")) gives SystemError: memory mapping failed: Invalid argument.

@simonster
Copy link
Member

An empty file is not valid JSON (see e.g. https://bugs.php.net/bug.php?id=68938), but the error could obviously be improved, and we should also throw an error for an empty string.

@IainNZ
Copy link
Contributor Author

IainNZ commented Mar 13, 2015

It was bad design/laziness on my part that led to even triggering this, but yeah, probably not the right error.

@IainNZ
Copy link
Contributor Author

IainNZ commented Aug 18, 2015

The error message is now

julia> JSON.parsefile("empty.json")
ERROR: ArgumentError: requested size must be > 0, got 0
 in mmap at mmap.jl:102
 in mmap at mmap.jl:153
 in anonymous at /Users/idunning/.julia/v0.4/JSON/src/JSON.jl:285
 in open at ./iostream.jl:114
 in parsefile at /Users/idunning/.julia/v0.4/JSON/src/JSON.jl:284

Which is easier to guess about, at least

@quinnj
Copy link
Member

quinnj commented Aug 18, 2015

The way we handle this in Base is open(io->mmap(io, T, dims, offset; grow=grow, shared=shared), file, isfile(file) ? "r+" : "w+")::Array{eltype(T),N}. Basically, if it's already a file, we read it, if it's not a file, we open a new one to write. Not sure if that same logic works here, but thought I'd mention it.

@TotalVerb
Copy link
Collaborator

With JuliaLang/julia@a6f4615 and #140, the error message would become:

julia> JSON.parsefile("empty.json")
ERROR: Unexpected end of input
Line: 0
Around: ......
           ^

 in _error(::String, ::JSON.Parser.MemoryParserState) at /home/fengyang/.julia/v0.5/JSON/src/Parser/Parser.jl:135
 in byteat at /home/fengyang/.julia/v0.5/JSON/src/Parser/Parser.jl:49 [inlined]
 in parse_value(::JSON.Parser.MemoryParserState, ::Type{T}) at /home/fengyang/.julia/v0.5/JSON/src/Parser/Parser.jl:155
 in #parse#1(::Type{Dict{String,Any}}, ::Function, ::String) at /home/fengyang/.julia/v0.5/JSON/src/Parser/Parser.jl:383
 in (::JSON.Parser.#kw##parse)(::Array{Any,1}, ::JSON.Parser.#parse, ::String) at ./null:0
 in (::JSON.##6#7{DataType,Bool,Int64})(::IOStream) at /home/fengyang/.julia/v0.5/JSON/src/JSON.jl:223
 in open(::JSON.##6#7{DataType,Bool,Int64}, ::String) at ./iostream.jl:113
 in parsefile(::String) at /home/fengyang/.julia/v0.5/JSON/src/JSON.jl:220
 in eval(::Module, ::Any) at ./boot.jl:225
 in macro expansion at ./REPL.jl:92 [inlined]
 in (::Base.REPL.##1#2{Base.REPL.REPLBackend})() at ./event.jl:46

which I think is quite reasonable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants