andrenth / ocaml-stdint

Various signed and unsigned integers for OCaml
Other
84 stars 15 forks source link

of_string for unsigned integers does not raise exception on negative inputs #47

Open anton-trunov opened 3 years ago

anton-trunov commented 3 years ago

Actual behavior

Here is a toplevel session:

μ> #require "stdint";;
μ> "-1" |> Stdint.Uint128.of_string |> Stdint.Uint128.to_string;;
- : string = "340282366920938463463374607431768211455"

Basically, this is the maximum 128-bit unsigned integer. It behaves the same for the rest of bit widths.

Expected behavior

Raises an exception according to the description here:

https://github.com/andrenth/ocaml-stdint/blob/48f97cd7e577a432064f33e4c2ad6859826716ad/lib/stdint.mli#L292-L297:

rgrinberg commented 3 years ago

Yes, that seems confusing. @rixed what do you think?

rixed commented 3 years ago

Given the quoted documentation and the fact that other Int modules seem to raise an exception (although not Failure but Invalid_arg, which looks better to me), I'd say that Uint128.of_string should indeed raise. I would favor if all of the of_string functions would raise the same exception BTW.