From ed6777e59f780a27cb00f7213fc70c05289325b2 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Mon, 3 Aug 2015 09:09:44 -0700 Subject: [PATCH] Fix json parsing i64::MIN, add tests for min and max i64 and u64 values --- serde_json/src/de.rs | 2 +- serde_tests/tests/test_json.rs | 25 ++++++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/serde_json/src/de.rs b/serde_json/src/de.rs index 4fbdd33d..26ab0703 100644 --- a/serde_json/src/de.rs +++ b/serde_json/src/de.rs @@ -167,7 +167,7 @@ impl Deserializer if pos { visitor.visit_u64(res) } else { - let res = -(res as i64); + let res = (res as i64).wrapping_neg(); // Make sure we didn't underflow. if res > 0 { diff --git a/serde_tests/tests/test_json.rs b/serde_tests/tests/test_json.rs index 3b6bfb61..69b1b7f8 100644 --- a/serde_tests/tests/test_json.rs +++ b/serde_tests/tests/test_json.rs @@ -1,6 +1,8 @@ use std::collections::BTreeMap; use std::fmt::Debug; +use std::i64; use std::marker::PhantomData; +use std::u64; use serde::de; use serde::ser; @@ -82,12 +84,23 @@ fn test_write_null() { test_pretty_encode_ok(tests); } +#[test] +fn test_write_u64() { + let tests = &[ + (3u64, "3"), + (u64::MAX, &u64::MAX.to_string()), + ]; + test_encode_ok(tests); + test_pretty_encode_ok(tests); +} + #[test] fn test_write_i64() { let tests = &[ (3i64, "3"), (-2i64, "-2"), (-1234i64, "-1234"), + (i64::MIN, &i64::MIN.to_string()), ]; test_encode_ok(tests); test_pretty_encode_ok(tests); @@ -95,11 +108,18 @@ fn test_write_i64() { #[test] fn test_write_f64() { + let min_string = f64::MIN.to_string(); + let max_string = f64::MAX.to_string(); + let epsilon_string = f64::EPSILON.to_string(); + let tests = &[ (3.0, "3.0"), (3.1, "3.1"), (-1.5, "-1.5"), (0.5, "0.5"), + (f64::MIN, &min_string), + (f64::MAX, &max_string), + (f64::EPSILON, &epsilon_string), ]; test_encode_ok(tests); test_pretty_encode_ok(tests); @@ -621,7 +641,7 @@ fn test_write_option() { ]); } -fn test_parse_ok(errors: Vec<(&'static str, T)>) +fn test_parse_ok(errors: Vec<(&str, T)>) where T: Clone + Debug + PartialEq + ser::Serialize + de::Deserialize, { for (s, value) in errors { @@ -718,6 +738,8 @@ fn test_parse_i64() { ("-2", -2), ("-1234", -1234), (" -1234 ", -1234), + (&i64::MIN.to_string(), i64::MIN), + (&i64::MAX.to_string(), i64::MAX), ]); } @@ -727,6 +749,7 @@ fn test_parse_u64() { ("0", 0u64), ("3", 3u64), ("1234", 1234), + (&u64::MAX.to_string(), u64::MAX), ]); }