callum-oakley / json5-rs

A Rust JSON5 serializer and deserializer which speaks Serde.
ISC License
184 stars 23 forks source link

from_str crashes when trying to parse... #11

Open ThePrzeglo opened 5 years ago

ThePrzeglo commented 5 years ago

Following code causes crash:

use json5::from_str;
use std::collections::HashMap;

#[macro_use]
extern crate serde_derive;

#[derive(Serialize, Deserialize, Debug, Hash, PartialEq, PartialOrd)]
struct Internal {
    x: Result<i32, Option<i32>>,
    y: Result<u32, Option<u32>>,
    x1: Result<i64, Option<i64>>,
    y1: Result<u64, Option<u64>>,
    tuple: Result<(u32, u32, u32), Option<(String, Vec<String>)>>,
}

#[derive(Serialize, Deserialize, Debug)]
struct Point {
    x: i32,
    y: u32,
    x1: i64,
    y1: u64,
    xz: usize,
    yz: usize,
    string: String,
    cnt_in_cnt: Vec<String>,
    cnt_in_cnt_in_cnt: Vec<Vec<String>>,
    data: Vec<u8>,
    float32: f32,
    float64: f64,
    tuple: (u32, u32, u32),
    internal: Internal,
    map: HashMap<String, Internal>,
    nested_map: Vec<HashMap<String, HashMap<String, HashMap<String, HashMap<String, HashMap<String, Internal>>>>>>,
}

fn main() {
    let data = "[[ ]]";
    let _: Result<Point, _> = json5::from_str(data);
}

Version in cargo.toml: json5 = "0.2.5"

Result:

thread 'main' panicked at 'byte index 2 is not a char boundary; it is inside '\u{a0}' (bytes 1..3) of `[ ]`', src/libcore/str/mod.rs:2046:5
stack backtrace:
   0: backtrace::backtrace::libunwind::trace
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.29/src/backtrace/libunwind.rs:88
   1: backtrace::backtrace::trace_unsynchronized
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.29/src/backtrace/mod.rs:66
   2: std::sys_common::backtrace::_print
             at src/libstd/sys_common/backtrace.rs:47
   3: std::sys_common::backtrace::print
             at src/libstd/sys_common/backtrace.rs:36
   4: std::panicking::default_hook::{{closure}}
             at src/libstd/panicking.rs:200
   5: std::panicking::default_hook
             at src/libstd/panicking.rs:214
   6: std::panicking::rust_panic_with_hook
             at src/libstd/panicking.rs:477
   7: std::panicking::continue_panic_fmt
             at src/libstd/panicking.rs:384
   8: rust_begin_unwind
             at src/libstd/panicking.rs:311
   9: core::panicking::panic_fmt
             at src/libcore/panicking.rs:85
  10: core::str::slice_error_fail
             at src/libcore/str/mod.rs:0
  11: core::str::traits::<impl core::slice::SliceIndex<str> for core::ops::range::RangeTo<usize>>::index::{{closure}}
             at /rustc/dddb7fca09dc817ba275602b950bb81a9032fb6d/src/libcore/str/mod.rs:1833
  12: core::option::Option<T>::unwrap_or_else
             at /rustc/dddb7fca09dc817ba275602b950bb81a9032fb6d/src/libcore/option.rs:419
  13: core::str::traits::<impl core::slice::SliceIndex<str> for core::ops::range::RangeTo<usize>>::index
             at /rustc/dddb7fca09dc817ba275602b950bb81a9032fb6d/src/libcore/str/mod.rs:1833
  14: core::str::traits::<impl core::ops::index::Index<I> for str>::index
             at /rustc/dddb7fca09dc817ba275602b950bb81a9032fb6d/src/libcore/str/mod.rs:1635
  15: json5::de::is_hex_literal
             at /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/json5-0.2.5/src/de.rs:283
  16: json5::de::parse_number
             at /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/json5-0.2.5/src/de.rs:250
  17: <&mut json5::de::Deserializer as serde::de::Deserializer>::deserialize_i32
             at /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/json5-0.2.5/src/de.rs:108
  18: serde::de::impls::<impl serde::de::Deserialize for i32>::deserialize
             at /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/serde-1.0.98/src/de/impls.rs:133
  19: <core::marker::PhantomData<T> as serde::de::DeserializeSeed>::deserialize
             at /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/serde-1.0.98/src/de/mod.rs:779
  20: <json5::de::Seq as serde::de::SeqAccess>::next_element_seed
             at /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/json5-0.2.5/src/de.rs:298
  21: serde::de::SeqAccess::next_element
             at /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/serde-1.0.98/src/de/mod.rs:1701
  22: <ossfuzz::_IMPL_DESERIALIZE_FOR_Point::<impl serde::de::Deserialize for ossfuzz::Point>::deserialize::__Visitor as serde::de::Visitor>::visit_seq
             at src/main.rs:16
  23: <&mut json5::de::Deserializer as serde::de::Deserializer>::deserialize_any
             at /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/json5-0.2.5/src/de.rs:61
  24: <&mut json5::de::Deserializer as serde::de::Deserializer>::deserialize_struct
             at ./<::serde::macros::forward_to_deserialize_any_method macros>:7
  25: ossfuzz::_IMPL_DESERIALIZE_FOR_Point::<impl serde::de::Deserialize for ossfuzz::Point>::deserialize
             at src/main.rs:16
  26: json5::de::from_str
             at /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/json5-0.2.5/src/de.rs:22
  27: ossfuzz::main
             at src/main.rs:39
  28: std::rt::lang_start::{{closure}}
             at /rustc/dddb7fca09dc817ba275602b950bb81a9032fb6d/src/libstd/rt.rs:64
  29: std::rt::lang_start_internal::{{closure}}
             at src/libstd/rt.rs:49
  30: std::panicking::try::do_call
             at src/libstd/panicking.rs:296
  31: __rust_maybe_catch_panic
             at src/libpanic_unwind/lib.rs:80
  32: std::panicking::try
             at src/libstd/panicking.rs:275
  33: std::panic::catch_unwind
             at src/libstd/panic.rs:394
  34: std::rt::lang_start_internal
             at src/libstd/rt.rs:48
  35: std::rt::lang_start
             at /rustc/dddb7fca09dc817ba275602b950bb81a9032fb6d/src/libstd/rt.rs:64
  36: main
  37: __libc_start_main
  38: _start
acanthite1855 commented 4 years ago

On my machine it works as expected: parse_str returns Err(Message("error parsing number")). Maybe it's something related to rust version and/or toolchain? (mine is rustc 1.42.0 (b8cedc004 2020-03-09) and toolchain stable-x86_64-pc-windows-msvc) @ThePrzeglo, what's your rust version and toolchain?

ThePrzeglo commented 4 years ago

I am pretty sure that I've tested it with some nightly like 1.40. I do not really want to browse my backups now to check this. On stable rustc 1.40.0 (73528e339 2019-12-16) with x86_64-unknown-linux-gnu works just fine.

Looks like it was false positive related to compiler. I think that this issue may be closed.