ocaml-stdint icon indicating copy to clipboard operation
ocaml-stdint copied to clipboard

of_string for unsigned integers does not raise exception on negative inputs

Open anton-trunov opened this issue 5 years ago • 2 comments

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:

anton-trunov avatar Oct 03 '20 19:10 anton-trunov

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

rgrinberg avatar Oct 03 '20 22:10 rgrinberg

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.

rixed avatar Oct 05 '20 13:10 rixed