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

Json ron deser large numbers error #91

Merged
merged 2 commits into from
Dec 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/serde_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -714,9 +714,9 @@ impl_ser_de_json_unsigned!(u32, core::u32::MAX);
impl_ser_de_json_unsigned!(u16, core::u16::MAX);
impl_ser_de_json_unsigned!(u8, core::u8::MAX);
impl_ser_de_json_signed!(i64, core::i64::MIN, core::i64::MAX);
impl_ser_de_json_signed!(i32, core::i64::MIN, core::i64::MAX);
impl_ser_de_json_signed!(i16, core::i64::MIN, core::i64::MAX);
impl_ser_de_json_signed!(i8, core::i64::MIN, core::i8::MAX);
impl_ser_de_json_signed!(i32, core::i32::MIN, core::i32::MAX);
impl_ser_de_json_signed!(i16, core::i16::MIN, core::i16::MAX);
impl_ser_de_json_signed!(i8, core::i8::MIN, core::i8::MAX);
impl_ser_de_json_float!(f64);
impl_ser_de_json_float!(f32);

Expand Down
6 changes: 3 additions & 3 deletions src/serde_ron.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,9 +754,9 @@ impl_ser_de_ron_unsigned!(u32, core::u32::MAX);
impl_ser_de_ron_unsigned!(u16, core::u16::MAX);
impl_ser_de_ron_unsigned!(u8, core::u8::MAX);
impl_ser_de_ron_signed!(i64, core::i64::MIN, core::i64::MAX);
impl_ser_de_ron_signed!(i32, core::i64::MIN, core::i64::MAX);
impl_ser_de_ron_signed!(i16, core::i64::MIN, core::i64::MAX);
impl_ser_de_ron_signed!(i8, core::i64::MIN, core::i8::MAX);
impl_ser_de_ron_signed!(i32, core::i32::MIN, core::i32::MAX);
impl_ser_de_ron_signed!(i16, core::i16::MIN, core::i16::MAX);
impl_ser_de_ron_signed!(i8, core::i8::MIN, core::i8::MAX);
impl_ser_de_ron_float!(f64);
impl_ser_de_ron_float!(f32);

Expand Down
27 changes: 27 additions & 0 deletions tests/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -842,3 +842,30 @@ fn array_leak_test() {

assert!(TOGGLED_ON_DROP.load(std::sync::atomic::Ordering::SeqCst))
}

// https://github.com/not-fl3/nanoserde/issues/89
#[test]
fn test_deser_oversized_value() {
use nanoserde::DeJson;

#[derive(DeJson, Clone, PartialEq, Debug)]
pub struct EnumConstant {
value: i32,
}
let max_json = format!(r#"{{"value": {} }}"#, i32::MAX);
let wrap_json = format!(r#"{{"value": {} }}"#, i32::MAX as i64 + 1);
assert_eq!(
<EnumConstant as DeJson>::deserialize_json(&max_json).unwrap(),
EnumConstant { value: i32::MAX }
);
assert_eq!(
<EnumConstant as DeJson>::deserialize_json(&wrap_json)
.unwrap_err()
.msg,
format!(
"Value out of range {}>{} ",
(i32::MAX as i64 + 1).to_string(),
i32::MAX.to_string()
)
);
}
28 changes: 28 additions & 0 deletions tests/ron.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,3 +457,31 @@ fn array_leak_test() {

assert!(TOGGLED_ON_DROP.load(std::sync::atomic::Ordering::SeqCst))
}

// https://github.com/not-fl3/nanoserde/issues/89
#[test]
fn test_deser_oversized_value() {
use nanoserde::DeRon;

#[derive(DeRon, Clone, PartialEq, Debug)]
pub struct EnumConstant {
value: i32,
}

let max_ron = format!(r#"(value:{})"#, i32::MAX);
let wrap_ron = format!(r#"(value:{})"#, i32::MAX as i64 + 1);
assert_eq!(
<EnumConstant as DeRon>::deserialize_ron(&max_ron).unwrap(),
EnumConstant { value: i32::MAX }
);
assert_eq!(
<EnumConstant as DeRon>::deserialize_ron(&wrap_ron)
.unwrap_err()
.msg,
format!(
"Value out of range {}>{} ",
(i32::MAX as i64 + 1).to_string(),
i32::MAX.to_string()
)
);
}
Loading