mlms13 / bs-decode

Type-safe JSON decoding for ReasonML and OCaml
https://mlms13.github.io/bs-decode/docs/what-and-why
MIT License
103 stars 18 forks source link

Int decoders succeed on out-of-range numbers #137

Closed mlms13 closed 10 months ago

mlms13 commented 1 year ago

I haven't actually confirmed this, but from looking at the code, it seems we only care about whether a number has a fractional part in our int decoder. We don't make any attempt to do bounds checking.

I think this means that a JS date that has been encoded as a JSON number will succeed at decoding as an int, but the int will overflow and the actual value won't match the original JSON.

My gut says that we could convert a number -> int -> float and compare with the original, and that should work? e.g.

let isValidInt = num =>
  float(int_of_float(num)) == num;

This compiles to:

function isValidInt(num) {
  return (num | 0) === num;
}

And as far as I can tell, this returns the correct result for out-of-bounds numbers, and numbers both with and without fractional parts.