Karim BELABAS on Mon, 4 Oct 1999 13:57:36 +0200 (MET DST)


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

Re: type() bug in gp


[Followup to my previous post:]

The type() function has always been "unsafe" in the sense that it let you
create meaningless objects that could completely corrupt a session. 

This patch adds consistency checks to prevent this.  It could be extended
for genuine type conversions (as done by Pol(), Vec(), Str(), ...). The
idea looks nice but I don't think it's worth the effort.

Karim.

Index: src/gp/highlvl.c
===================================================================
RCS file: /home/megrez/cvsroot/pari/src/gp/highlvl.c,v
retrieving revision 1.1.1.1
diff -c -r1.1.1.1 highlvl.c
*** src/gp/highlvl.c	1999/09/16 13:47:42	1.1.1.1
--- src/gp/highlvl.c	1999/10/04 11:25:52
***************
*** 163,175 ****
  GEN
  type0(GEN x, char *st)
  {
    if (! *st) 
    {
      char *s = type_name(typ(x));
      return strtoGENstr(s, 0);
    }
!   x = gcopy(x); settyp(x,get_type_num(st));
!   return x;
  }
  
  entree functions_highlevel[]={
--- 163,204 ----
  GEN
  type0(GEN x, char *st)
  {
+   long t, tx;
    if (! *st) 
    {
      char *s = type_name(typ(x));
      return strtoGENstr(s, 0);
    }
!   tx = typ(x);
!   t = get_type_num(st);
! 
!   if (is_frac_t(tx))
!   {
!     if (!is_frac_t(t) && !is_rfrac_t(t))
!       err(typeer, "type");
!     x = gcopy(x);
!   }
!   else if (is_rfrac_t(tx))
!   {
!     if (is_frac_t(t))
!     {
!       x = gred_rfrac(x);
!       tx = typ(x);
!       if (!is_frac_t(tx)) err(typeer, "type");
!     }
!     else
!     {
!       if (!is_rfrac_t(t)) err(typeer, "type");
!       x = gcopy(x);
!     }
!   }
!   else if (is_vec_t(tx))
!   {
!     if (!is_vec_t(t)) err(typeer, "type");
!     x = gcopy(x);
!   }
!   else if (tx != t) err(typeer, "type");
!   settyp(x, t); return x;
  }
  
  entree functions_highlevel[]={
Index: doc/usersch3.tex
===================================================================
RCS file: /home/megrez/cvsroot/pari/doc/usersch3.tex,v
retrieving revision 1.5
diff -c -r1.5 usersch3.tex
*** doc/usersch3.tex	1999/09/29 13:47:48	1.5
--- doc/usersch3.tex	1999/10/04 11:25:59
***************
*** 6473,6494 ****
  part here, so that \kbd{FRACN} by itself would also be all right). Check out
  existing type names with the metacommand \b{t}.\label{se:gptype}
  
!    Type changes must be used with extreme caution, or disasters may
! occur (\kbd{SIGSEGV} or \kbd{SIGBUS} being one's best bet), but one instance
! where it can be useful is \kbd{type(x,RFRACN)} when \kbd{x} is a rational
! function (type \typ{RFRAC}). In this case, the created object, as well as
! the objects created from it, will not be reduced automatically, making the
! operations much faster. In fact this function is the {\it only\/} way to create
  reducible rationals (type \typ{FRACN}) or rational functions (type
! \typ{RFRACN}) in GP.
  
  There is no equivalent library syntax, since the internal functions \kbd{typ}
! and \kbd{settyp} are available. Note that \kbd{settyp} does {\it not\/} create
! a copy of \kbd{x}, contrary to most PARI functions. It just changes the type in
! place (and returns nothing). \kbd{typ} returns a C long integer. Note also
! the different spellings of the internal functions (\kbd{set})\kbd{typ} and of
! the GP function \kbd{type}\footnote{*}{This is due to the fact that
! \kbd{type} is a reserved identifier for some C compilers.}.
  
  \subsubsecidx{whatnow}$(\var{key})$: if keyword \var{key} is the name
  of a function that was present in GP version 1.39.15 or lower, outputs
--- 6473,6493 ----
  part here, so that \kbd{FRACN} by itself would also be all right). Check out
  existing type names with the metacommand \b{t}.\label{se:gptype}
  
!   GP won't let you create meaningless objects in this way where the internal
! structure doesn't match the type. This function can be useful to create
  reducible rationals (type \typ{FRACN}) or rational functions (type
! \typ{RFRACN}). In fact it's the only way to do so in GP. In this case, the
! created object, as well as the objects created from it, will not be reduced
! automatically, making some operations a bit faster.
  
  There is no equivalent library syntax, since the internal functions \kbd{typ}
! and \kbd{settyp} are available. Note that \kbd{settyp} does {\it not\/}
! create a copy of \kbd{x}, contrary to most PARI functions. It also doesn't
! check for consistency. \kbd{settyp} just changes the type in place and
! returns nothing. \kbd{typ} returns a C long integer. Note also the different
! spellings of the internal functions (\kbd{set})\kbd{typ} and of the GP
! function \kbd{type}, which is due to the fact that \kbd{type} is a reserved
! identifier for some C compilers.
  
  \subsubsecidx{whatnow}$(\var{key})$: if keyword \var{key} is the name
  of a function that was present in GP version 1.39.15 or lower, outputs

__
Karim Belabas                    email: Karim.Belabas@math.u-psud.fr
Dep. de Mathematiques, Bat. 425
Universite Paris-Sud             Tel: (00 33) 1 69 15 57 48
F-91405 Orsay (France)           Fax: (00 33) 1 69 15 60 19
--
PARI/GP Home Page: http://hasse.mathematik.tu-muenchen.de/ntsw/pari/