-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
bigint add failures with aliasing #8330
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
CI green (modulo FreeBSD CI outage), should be good now. |
Our Discord friend has found another bug of the same class, so marking as a draft until that's fixed too. Seems to happen when we hit the 2192 limit, again only with aliasing. |
Lines 1699 to 1707 in 749c3f0
If Lines 1472 to 1477 in 749c3f0
The memory pointed to by the alias is no longer valid. |
Ref #6167 (comment). |
CI green again. |
Rebased and brought back up to date. |
bigint add failures with aliasing
A Discord user came to us with a failing use of bigint which was just calculating the Fibonacci sequence.
It turns out it was miscalculating on the jump between one and two limbs. See attached a test that fails currently, but should succeed. It is not written very elegantly, but it's easy to verify that it should work.
It looks like the offending code is this optimisation in
Mutable.add
:zig/lib/std/math/big/int.zig
Lines 318 to 324 in dce612a
Note that, if
@addWithOverflow
does overflow,add
continues on to perform Knuth's algorithm inldadd
, but it has also overwritten the contents ofr.limbs[0]
here. Ifr
is aliased with one of its inputs, this causes the subsequent non-optimised calculation to fail.I've confirmed the test passes if this is removed. We may want to do an alias check and skip the optimisation if overwriting
r
with garbage here will be a problem.edit: I've added a suggested fix in case it's the right one, which skips this
@addWithOverflow
attempt ifr.limbs.ptr
is equal toa.limbs.ptr
orb.limbs.ptr
./cc @alexnask fyi