mpz_limbs_modify doesn't clear new limbs

Ivo maffei ivomaffei at gmail.com
Thu Jan 2 11:41:51 CET 2025


This is indeed my issue.

If I call xptr = mpz_limbs_modify(x,n), then xptr[0..mpz_size(x)) is the
value of x, but xptr[0..n) might contain the unused limbs.
When reading the documentation, I was expecting the whole array xptr[0..n)
to represent the value of x, but this is not the case.
As a side effect, the code ``mpz_limbs_modify(x,n); mpz_limbs_finish(x,
n);'' can alter the value of x even if n > mpz_size(x).
The above "feels wrong", but if it's not considered a bug, then perhaps the
manual should make this clear for newbies like me.

- Ivo

On Thu, Jan 2, 2025 at 11:07 AM Vincent Lefevre <vincent at vinc17.net> wrote:

> Hi,
>
> On 2025-01-02 10:31:55 +0100, Ivo maffei wrote:
> > In both cases, the most significant limb might not be zero.
> > So, is this a bug or mpz_limbs_modify was never meant to zero the new
> limbs?
>
> Note that not all *allocated* limbs are used, and I don't think
> that unused limbs are required to be zero. See
>
>   16.1 Integer Internals
>
> and _mp_size vs _mp_alloc.
>
> --
> Vincent Lefèvre <vincent at vinc17.net> - Web: <https://www.vinc17.net/>
> 100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
> Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)
>


More information about the gmp-discuss mailing list