Nix integer overflow is implemented by undefined behavior
Describe the bug
See https://git.lix.systems/lix-project/lix/issues/423
- Nix currently behaves by overflowing.
- Overflowing is UB, but predictable, so a few users might rely on it without difficulties such as impurities showing through hashes and substitution
Steps To Reproduce
nix-repl> 500000000*50000000000
6553255926290448384
Expected behavior
I'm inclined to make it throw instead. If that is a problem, we may revisit. Have some thoughts on how; see https://git.lix.systems/lix-project/lix/issues/423#issuecomment-4604
nix-env --version output
Additional context
Priorities
Add :+1: to issues you find important.
This issue has been mentioned on NixOS Discourse. There might be relevant details there:
https://discourse.nixos.org/t/2024-06-26-nix-team-meeting-minutes-156/47740/1
nix-repl> :p with lib.prng; iterate xoshiro256ss.generate (xoshiro256ss.initState 0) 5
[
[
«error: integer overflow in adding -7046029254386353131 + -7046029254386353131»
«error: integer overflow in adding -7046029254386353131 + -7046029254386353131»
«error: integer overflow in adding -7046029254386353131 + -7046029254386353131»
«error: integer overflow in adding -7046029254386353131 + -7046029254386353131»
«error: integer overflow in adding -7046029254386353131 + -7046029254386353131»
]
[
«error: integer overflow in multiplying 7046029256649317107 * -4658895280553007687»
«error: integer overflow in adding -7046029254386353131 + -7046029254386353131»
«error: integer overflow in adding -7046029254386353131 + -7046029254386353131»
«error: integer overflow in adding -7046029254386353131 + -7046029254386353131»
]
]
i am very sad :(
This issue has been mentioned on NixOS Discourse. There might be relevant details there:
https://discourse.nixos.org/t/nix-2-25-released/55994/1
nix-repl> :p with lib.prng; iterate xoshiro256ss.generate (xoshiro256ss.initState 0) 5 [ [ «error: integer overflow in adding -7046029254386353131 + -7046029254386353131» «error: integer overflow in adding -7046029254386353131 + -7046029254386353131» «error: integer overflow in adding -7046029254386353131 + -7046029254386353131» «error: integer overflow in adding -7046029254386353131 + -7046029254386353131» «error: integer overflow in adding -7046029254386353131 + -7046029254386353131» ] [ «error: integer overflow in multiplying 7046029256649317107 * -4658895280553007687» «error: integer overflow in adding -7046029254386353131 + -7046029254386353131» «error: integer overflow in adding -7046029254386353131 + -7046029254386353131» «error: integer overflow in adding -7046029254386353131 + -7046029254386353131» ] ]i am very sad :(
I'm terribly sorry to hear. Either this should have an intentionally wrapping built-in (maybe. I'm not of the opinion that writing bit twiddling in nix language is a reasonable use case that particularly needs encouraging) or, more likely, it should have been simply written mod 2^32, which, as an added bonus, eliminates overflowing becoming negative.
Either way this code has been broken in Lix for five months and I've not heard of it before today. The library in question is not in nixpkgs, and a vanishingly small number of people use it. Thus, IMO no language accommodations should be made for it given it's a niche use case and can individually be fixed.