Ilya Zakharevich on Tue, 19 Sep 2000 17:07:47 -0400

[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: Math::Pari segfaults?

On Mon, Sep 18, 2000 at 01:00:58PM +0530, Vipul Ved Prakash wrote:
> After a few wild goose chases, I discovered the bug.  makerandom_itv() (in
> Crypt::Random) was doing $c = Mod(0, $b-$a); -- this statement was
> triggering the segfault.  I changed it $d = $b-$a; $c = Mod(0, $d); and now
> it seems to work fine.  ($a,$b,$c,$d are Math::Pari objects.)
> While this seems to have something to do with PARI's non-existent
> refcounting mechanism, I don't understand exactly why $c = Mod(0, $b-$a) was
> breaking.  I would appreciate if you could explain.

Hint (this is not a full explanation: the actual bug is due to
interaction of several different design choices, this shows only one
of the parts):

 Initially $b-$a was a temporary, which was promptly deleted after the
 statement $c = Mod(0, $b-$a); was executed.  After it was deleted,
 one of the pointers in the internal representation of $c became
 invalid (due to absense of refcounting in PARI).

The change you did put the modulus into a long-time storage, so it
continued to be valid during the lifetime of $c.

[In fact I do not know *in details* how $c = Mod(0, $b-$a); could lead
 to a failure.  I would think that in this particular example PARI
 could understand that $b-$a is a temporary (I would think it should
 live on the PARI stack...).  I need to think more about this.]