Skip to content

Commit

Permalink
Fix bounds on short integer storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Diggsey committed Apr 20, 2021
1 parent 18450b4 commit e79f1d3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ijson"
version = "0.1.0"
version = "0.1.1"
authors = ["Diggory Blake <diggsey@googlemail.com>"]
edition = "2018"
readme = "README.md"
Expand Down
27 changes: 26 additions & 1 deletion src/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,10 @@ const STATIC_UPPER: i16 = STATIC_LOWER + STATIC_LEN as i16;
static STATIC_NUMBERS: [Header; STATIC_LEN] =
define_static_numbers!(STATIC_LOWER 0 1 2 3 4 5 6 7 8);

// Range of a 24-bit signed integer.
const SHORT_LOWER: i64 = -0x800000;
const SHORT_UPPER: i64 = 0x800000;

/// The `INumber` type represents a JSON number. It is decoupled from any specific
/// representation, and internally uses several. There is no way to determine the
/// internal representation: instead the caller is expected to convert the number
Expand Down Expand Up @@ -426,7 +430,7 @@ impl INumber {
}

fn new_i64(value: i64) -> Self {
if value as u64 & 0xFFFFFFFFFF000000 == 0 {
if value >= SHORT_LOWER && value < SHORT_UPPER {
Self::new_short(value as i32)
} else {
let mut res = Self::new_ptr(NumberType::I64);
Expand Down Expand Up @@ -714,6 +718,27 @@ mod tests {
assert_eq!(x.to_i64(), None);
assert_eq!(x.to_u64(), Some(u64::MAX));
assert_eq!(x.to_f64(), None);

let x: INumber = 13369629.into();
assert_eq!(x.to_i64(), Some(13369629));
assert_eq!(x.to_u64(), Some(13369629));
assert_eq!(x.to_f64(), Some(13369629.0));

let x: INumber = 0x800000.into();
assert_eq!(x.to_i64(), Some(0x800000));
assert_eq!(x.to_u64(), Some(0x800000));

let x: INumber = (-0x800000).into();
assert_eq!(x.to_i64(), Some(-0x800000));
assert_eq!(x.to_u64(), None);

let x: INumber = 0x7FFFFF.into();
assert_eq!(x.to_i64(), Some(0x7FFFFF));
assert_eq!(x.to_u64(), Some(0x7FFFFF));

let x: INumber = (-0x7FFFFF).into();
assert_eq!(x.to_i64(), Some(-0x7FFFFF));
assert_eq!(x.to_u64(), None);
}

#[mockalloc::test]
Expand Down

0 comments on commit e79f1d3

Please sign in to comment.