From p.dalgaard@biostat.ku.dk  Tue Dec 11 23:33:01 2001
From: p.dalgaard@biostat.ku.dk (Peter Dalgaard BSA)
Date: 12 Dec 2001 00:33:01 +0100
Subject: [Rd] crash bug in get("function.name")() (PR#1211)
In-Reply-To: <3C16912C.75907CDA@statistik.uni-dortmund.de>
References: <200112112232.XAA15690@pubhealth.ku.dk>
 <3C16912C.75907CDA@statistik.uni-dortmund.de>
Message-ID: <x2adwps576.fsf@blueberry.kubism.ku.dk>

Uwe Ligges <ligges@statistik.uni-dortmund.de> writes:

> rmh@surfer.sbm.temple.edu wrote:
> > 
> > R : Copyright 2001, The R Development Core Team
> > Version 1.3.1  (2001-08-31)
> > 
> > > tmp <- factor(1:3)
> > > get("print.factor")(tmp)
> > [1] 1 2 3
> > Levels:  1 2 3
> > > print.a <- function(x,...) {
> > +   print("this is a")
> > +   NextMethod("print", x, quote = FALSE, right = TRUE, ...)
> > + }
> > >
> > > get("print.a")
> > function(x,...) {
> >   print("this is a")
> >   NextMethod("print", x, quote = FALSE, right = TRUE, ...)
> > }
> > > print.a(tmp)
> > [1] "this is a"
> > [1] 1 2 3
> > Levels:  1 2 3
> > > get("print.a")(tmp)
> > [1] "this is a"
> > 
> > Process R exited abnormally with code 5 at Tue Dec 11 16:18:17 2001
> > 
> > ----
> > 
> > I am using the windows version rw1031
> > 
> > I also tried on rw1030.   rw1030 gives a different code
> > Process R exited abnormally with code 255 at Tue Dec 11 17:31:06 2001
> 
> 
> With an existing workspace the same happens with R-1.4.0 (unstable) on
> WinNT4.
> 
> Also in R-1.4.0 (unstable) the last lines of your example, if started
> without an existing workspace, results in:
> 
> >  print.a(tmp)
> [1] "this is a"
> [1] 1 2 3
> Levels:  1 2 3 
> >  get("print.a")(tmp)
> [1] "this is a"
> Error in get("print.a")(tmp) : function is not a closure
> >  get("print.a")
> Error: attempt to apply non-function
> >  get 
> #### Now a crash!

This comes from this spot:

(gdb) 
#3  0x080cc4cc in do_nextmethod (call=0x873cc68, op=0x8214ee8,args=0x873cd2c, 
    env=0x8b2f394) at ../../../R/src/main/objects.c:488
488         s = findFun(CAR(cptr->call), cptr->sysparent);

CAR(cptr->call) grabs the first element of the call. In a construction
like get("print.a")(tmp) that will be  get("print.a"). However, findFun
assumes that it is being passed a symbol, and pokes a HASHVALUE into
what it thinks is the PRINTNAME of the object, and all hell breaks
loose. 

However, given the way that method dispatch works (by "dotting on" the
class name to the function name), I don't think this kind of
construction makes sense at all.  

There probably needs to be a check in findFun that its symbol argument
really is one. It is however of the "Doctor, it hurts when I do this"
variety, and it might be better to keep our hands off it this close to
release time.

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._


[Rd] crash bug in get("function.name")() (PR#1211)
Peter Dalgaard BSA