GMP Bug Report
marco.bodrato at tutanota.com
marco.bodrato at tutanota.com
Sat Mar 1 13:47:34 CET 2025
Ciao,
22 feb 2025, 11:18 da kuntz at shaw.ca:
> The bug likely exists because the source uses "int" to store sizes,
>
I do not agree. The behavior is a consequence of the file format.
> for example in this macro:
>
> #ifndef HTON_LIMB_STORE
> #define HTON_LIMB_STORE(dst, limb) \
>
[...]
> int __i; \
> for (__i = 0; __i < GMP_LIMB_BYTES; __i++) \
>
here the int variable is correctly used for numbers between 0 and GMP_LIMB_BYTES (typically 4 or 8),
> There is also an "int bits;" on line 107.
>
where bits count the bits in a "limb", (typically 32 or 64).
No the issue is not triggered by the use of the type "int" in the code, but by the (documented) file format "4 bytes of size information, and that many bytes of limbs", plus the (not so well documented) fact the "4 bytes of size" also have to store also the sign.
> From: "James Cumberbatch"
>
[...]
> Description of what is wrong: When using mpz_inp_raw to load a number
> which is at least 2^(2^34), a different value to the one which is
> written is loaded. Arithmetic works fine with these numbers and
> doesn't go wrong until larger numbers where gmp doesn't claim to work,
> only loading the number from a file goes wrong. (Or theoretically the
> error might be in mpz_out_raw, but doing a hexdump it looks like it's
>
Well, yes, I believe the bug is in the documentation, because we should underline that the file format is only suitable for numbers which absolute value is strictly smaller than 2^(8*(2^31-1)), and in mpz_out_raw, that should rise an error (I mean return 0) if number can not be represented by the file format.
Maybe we should at least correct mpz_out_raw, to return 0 when the number is too large. I attach a possible patch.
Then, we might need a new in/out couple of functions with a larger file format.
Ĝis,mb
-------------- next part --------------
A non-text attachment was scrubbed...
Name: out_raw.patch
Type: text/x-patch
Size: 1197 bytes
Desc: not available
URL: <https://gmplib.org/list-archives/gmp-bugs/attachments/20250301/15204e21/attachment.bin>
More information about the gmp-bugs
mailing list