Ilya Zakharevich on Sun, 17 Sep 2000 15:57:47 -0400

 Re: Math::Pari segfaults?

```On Sat, Sep 16, 2000 at 11:32:02PM -0700, Vipul Ved Prakash wrote:
> Greetings Ilya!
>
> You might recall that I was bugging you last year around the same time about
> a Math::Pari bug.  Well, recently I resurrected Crypt::Primes which uses
> Math::Pari to generate large, random prime numbers.  Sometimes, while
> generating 1024-bit primes, the module segfaults.  Here's a core-file
> backtrace:

This summer I have been debugging a similar problem.  My conclusion was:
this is the problem sitting deeply in PARI semantic.  Maybe your
problem is the same.

PARI's Mod(a,b) may reference the b modulus in several ways, one of
them being that the object for Mod(a,b) just keeps the pointer to b.
*Without increasing the refcount of b* (there is no notion of refcount
in PARI!).  The logic of Math::Pari is such that you cannot control
whether this way of storing the modulus is used.

Corollary for Math::Pari: if you use \$c = Mod(\$a,\$b), \$b should live
strictly longer than \$b.  Given to a bug/misfeature of Perl, it is not
enough to have

{
my \$b = Blah();
my \$c = Mod(\$a,\$b);
...
}

or even

{
my \$b = Blah();
{
my \$c = Mod(\$a,\$b);
...
}
}

You need an intervening statement between the ends of two blocks:

{
my \$b = Blah();
{
my \$c = Mod(\$a,\$b);
...
}
\$fake++;			# Allow \$c to be destructed before \$a
}

Another solution is to have \$b in a file-scope variable, and \$c in a
subroutine-scope variable.

Hope this helps,
Ilya
```