quinnj / JSON3.jl

Other
214 stars 47 forks source link

Stack overflow when writing `Real` that isn't `AbstractFloat` or `Integer` #232

Open iamed2 opened 2 years ago

iamed2 commented 2 years ago

Examples:

julia> JSON3.write(3//2)
ERROR: StackOverflowError:
Stacktrace:
     [1] write(::StructTypes.NumberType, buf::Vector{UInt8}, pos::Int64, len::Int64, x::Rational{Int64}; kw::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
       @ JSON3 ~/.julia/packages/JSON3/vguNZ/src/write.jl:213
     [2] write(::StructTypes.NumberType, buf::Vector{UInt8}, pos::Int64, len::Int64, x::Rational{Int64})
       @ JSON3 ~/.julia/packages/JSON3/vguNZ/src/write.jl:213
--- the last 2 lines are repeated 39990 more times ---
 [79983] write(::StructTypes.NumberType, buf::Vector{UInt8}, pos::Int64, len::Int64, x::Rational{Int64}; kw::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
       @ JSON3 ~/.julia/packages/JSON3/vguNZ/src/write.jl:213
julia> using FixedPointDecimals

julia> JSON3.write(FixedDecimal{Int,2}(32.14))
ERROR: StackOverflowError:
Stacktrace:
     [1] write(::StructTypes.NumberType, buf::Vector{UInt8}, pos::Int64, len::Int64, x::FixedDecimal{Int64, 2}; kw::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
       @ JSON3 ~/.julia/packages/JSON3/vguNZ/src/write.jl:213
     [2] write(::StructTypes.NumberType, buf::Vector{UInt8}, pos::Int64, len::Int64, x::FixedDecimal{Int64, 2})
       @ JSON3 ~/.julia/packages/JSON3/vguNZ/src/write.jl:213
--- the last 2 lines are repeated 39990 more times ---
 [79983] write(::StructTypes.NumberType, buf::Vector{UInt8}, pos::Int64, len::Int64, x::FixedDecimal{Int64, 2}; kw::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
       @ JSON3 ~/.julia/packages/JSON3/vguNZ/src/write.jl:213
julia> JSON3.write(π)
ERROR: StackOverflowError:
Stacktrace:
     [1] write(::StructTypes.NumberType, buf::Vector{UInt8}, pos::Int64, len::Int64, x::Irrational{:π}; kw::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
       @ JSON3 ~/.julia/packages/JSON3/vguNZ/src/write.jl:213
     [2] write(::StructTypes.NumberType, buf::Vector{UInt8}, pos::Int64, len::Int64, x::Irrational{:π})
       @ JSON3 ~/.julia/packages/JSON3/vguNZ/src/write.jl:213
--- the last 2 lines are repeated 39990 more times ---
 [79983] write(::StructTypes.NumberType, buf::Vector{UInt8}, pos::Int64, len::Int64, x::Irrational{:π}; kw::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
       @ JSON3 ~/.julia/packages/JSON3/vguNZ/src/write.jl:213
quinnj commented 2 years ago

Ok, proposal up: https://github.com/quinnj/JSON3.jl/pull/236. @iamed2, would like to hear your thoughts on the proposed solution.