Karim BELABAS on Tue, 8 Jul 2003 19:58:36 +0200 (MEST)


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

Re: variable corruption bug


On Sun, 6 Jul 2003, Igor Schein wrote:
> ? f()=polredabs(p=x)
> ? g(p)=for(k=p,p,f())
> ? g()
>   ***   bug in gadd, please report
>
> No mechanism in place to detect the improver variable use.

Not really. The real problem is that GP variables do not use reference
counts. Here p = x in f() frees the memory block occupied by p's former
value, which happens to be pointed to by k [ since for( k = p...) ].

I can easily fix the above by replacing the pointer copy in forpari() by a
full copy, but it would not be a very good fix. [ Has to be done in many
other places, and is still unsafe ]

Adding reference counts at this point is relatively tough because a single
block is allocated to a variable's value, and other variables / objects may
point to components of the block ( not to the root in most cases ).

This would force us to introduce into the "entree" struct a pointer to the
root of the allocated object, in addition to the actual value. This has
to be done consistently, in many places.

The simple alternative is to replace pointer copy by a full copy (aka gclone)
so that pointing to a limb of something else is now impossible. This would
make simple loops about twice slower ( I made the measurements when I
introduced the pointer copy, about 8 years ago ). Not nice.

Adding reference counts is a sound idea, and certainly should be done, but I
won't undertake this.

    Karim.
-- 
Karim Belabas                     Tel: (+33) (0)1 69 15 57 48
Dép. de Mathématiques, Bât. 425   Fax: (+33) (0)1 69 15 60 19
Université Paris-Sud              http://www.math.u-psud.fr/~belabas/
F-91405 Orsay (France)            http://www.parigp-home.de/  [PARI/GP]