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

Bugfix: lax_deserialize currently accepts deserializing from NaN #25

Closed
wants to merge 7 commits into from
Closed
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
10 changes: 7 additions & 3 deletions .github/workflows/stable.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: test
args: --features std,serde,lax_deserialize
- name: Run external tests
- name: Run tests (serde)
uses: actions-rs/cargo@v1
with:
command: test
args: -p js_int_ext_tests --all-features
args: --features serde
- name: Run tests (all features)
uses: actions-rs/cargo@v1
with:
command: test
args: --all-features
- name: Run tests (release build)
uses: actions-rs/cargo@v1
with:
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ lax_deserialize = ["serde"]
default = ["std"]
std = []

[workspace]
members = ["ext_tests"]
[dev-dependencies]
serde_test = "1.0"
16 changes: 0 additions & 16 deletions ext_tests/Cargo.toml

This file was deleted.

6 changes: 0 additions & 6 deletions ext_tests/README.md

This file was deleted.

82 changes: 0 additions & 82 deletions ext_tests/src/lib.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ impl<'de> Deserialize<'de> for Int {
{
let val = f64::deserialize(deserializer)?;

if val > MAX_SAFE_INT as f64 || val < MIN_SAFE_INT as f64 {
if val > MAX_SAFE_INT as f64 || val < MIN_SAFE_INT as f64 || val.is_nan() {
Err(D::Error::invalid_value(
Unexpected::Float(val),
&"a number between -2^53 + 1 and 2^53 - 1",
Expand Down
2 changes: 1 addition & 1 deletion src/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ impl<'de> Deserialize<'de> for UInt {
{
let val = f64::deserialize(deserializer)?;

if val < 0.0 || val > MAX_SAFE_UINT as f64 {
if val < 0.0 || val > MAX_SAFE_UINT as f64 || val.is_nan() {
Err(D::Error::invalid_value(
Unexpected::Float(val),
&"a number between 0 and 2^53 - 1",
Expand Down
64 changes: 64 additions & 0 deletions tests/int.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#![cfg(feature = "serde")]

use js_int::{int, Int};
use serde::{de::IntoDeserializer, Deserialize};
use serde_test::{assert_ser_tokens, Token};

#[test]
fn serialize_int() {
assert_serialize(100);
assert_serialize(0);
assert_serialize(-100);
}

fn assert_serialize(number: i32) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was tempted to use #[track_caller] here but then I remembered that the MSRV is 1.35.

Using a macro would also be possible but I don't think it's really worth it.

assert_ser_tokens(
&Int::from(number),
&[Token::NewtypeStruct { name: "Int" }, Token::I64(number as _)],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Writing out the NewTypeStruct serialization token I wonder: Is this behavior actually intended? Should a Serializer really know both the name of the struct and it's internal workings?

This relates to #16 because a manual Serialize implementation could avoid leaking such implementation details.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. Idk which serializers out there actually serialize sth. for NewtypeStruct, but I agree that we shouldn't emit it. If the derives aren't to be replaced entirely, #[serde(transparent)] could be used to get rid of it.

)
}

#[test]
fn deserialize_int() {
assert_eq!(deserialize_from(100).unwrap(), int!(100));
assert_eq!(deserialize_from(0).unwrap(), int!(0));
assert_eq!(deserialize_from(-100).unwrap(), int!(-100));
assert_eq!(deserialize_from(-9007199254740991i64).unwrap(), Int::MIN);
assert_eq!(deserialize_from(9007199254740991i64).unwrap(), Int::MAX);
assert!(deserialize_from(9007199254740992i64).is_err());
assert!(deserialize_from(-9007199254740992i64).is_err());
}

#[test]
#[cfg_attr(feature = "lax_deserialize", ignore)]
fn strict_deserialize_int() {
assert!(deserialize_from(-10.0).is_err());
assert!(deserialize_from(-0.0).is_err());
assert!(deserialize_from(0.5).is_err());
assert!(deserialize_from(1.0).is_err());
assert!(deserialize_from(9007199254740991.0).is_err());
assert!(deserialize_from(9007199254740991.49).is_err());
assert!(deserialize_from(9007199254740992.0).is_err());
}

#[test]
#[cfg_attr(not(feature = "lax_deserialize"), ignore)]
fn lax_deserialize_int() {
assert_eq!(deserialize_from(-10.0).unwrap(), int!(-10));
assert_eq!(deserialize_from(-0.0).unwrap(), int!(0));
assert_eq!(deserialize_from(0.5).unwrap(), int!(0));
assert_eq!(deserialize_from(1.0).unwrap(), int!(1));
assert_eq!(deserialize_from(9007199254740991.0).unwrap(), Int::MAX);
assert_eq!(deserialize_from(9007199254740991.49).unwrap(), Int::MAX);
assert!(deserialize_from(9007199254740992.0).is_err());

assert!(deserialize_from(f64::NAN).is_err());
assert!(deserialize_from(f64::INFINITY).is_err());
assert!(deserialize_from(f64::NEG_INFINITY).is_err());
}

fn deserialize_from<'de, Value: IntoDeserializer<'de>>(
value: Value,
) -> Result<Int, serde::de::value::Error> {
Int::deserialize(value.into_deserializer())
}
56 changes: 56 additions & 0 deletions tests/uint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#![cfg(feature = "serde")]

use js_int::{uint, UInt};
use serde::{de::IntoDeserializer, Deserialize};
use serde_test::{assert_ser_tokens, Token};

#[test]
fn serialize_uint() {
assert_serialize(100);
assert_serialize(0);
}

fn assert_serialize(number: u32) {
assert_ser_tokens(
&UInt::from(number),
&[Token::NewtypeStruct { name: "UInt" }, Token::U64(number as _)],
)
}

#[test]
fn deserialize_uint() {
assert_eq!(deserialize_uint_from(100).unwrap(), uint!(100));
assert_eq!(deserialize_uint_from(0).unwrap(), uint!(0));
assert_eq!(deserialize_uint_from(9007199254740991i64).unwrap(), UInt::MAX);
assert!(deserialize_uint_from(9007199254740992i64).is_err());
}

#[test]
#[cfg_attr(feature = "lax_deserialize", ignore)]
fn strict_deserialize_uint() {
assert!(deserialize_uint_from(0.5).is_err());
assert!(deserialize_uint_from(1.0).is_err());
assert!(deserialize_uint_from(9007199254740991.0).is_err());
assert!(deserialize_uint_from(9007199254740991.49).is_err());
assert!(deserialize_uint_from(9007199254740992.0).is_err());
}

#[test]
#[cfg_attr(not(feature = "lax_deserialize"), ignore)]
fn lax_deserialize_uint() {
assert_eq!(deserialize_uint_from(0.5).unwrap(), uint!(0));
assert_eq!(deserialize_uint_from(1.0).unwrap(), uint!(1));
assert_eq!(deserialize_uint_from(9007199254740991.0).unwrap(), UInt::MAX);
assert_eq!(deserialize_uint_from(9007199254740991.49).unwrap(), UInt::MAX);
assert!(deserialize_uint_from(9007199254740992.0).is_err());

assert!(deserialize_uint_from(f64::NAN).is_err());
assert!(deserialize_uint_from(f64::INFINITY).is_err());
assert!(deserialize_uint_from(f64::NEG_INFINITY).is_err());
}

fn deserialize_uint_from<'de, Value: IntoDeserializer<'de>>(
value: Value,
) -> Result<UInt, serde::de::value::Error> {
UInt::deserialize(value.into_deserializer())
}