[Rd] NextMethod causes R 2.2.0 to crash (PR#8416)

Henrik Bengtsson hb at maths.lth.se
Wed Dec 21 04:48:24 CET 2005


Bill.Venables at csiro.au wrote:
> I found writing the following default method the for the generic
> function "julian" causes R to crash.
> 
> 
> julian.default <- function(x, ...) {
>         x <- as.Date(x)
>         NextMethod("julian", x, ...)
> }

On Windows XP R 2.2.0 Patched (2005-11-21 r36410) you get:

Error: evaluation nested too deeply: infinite recursion / 
options(expressions=)?

and on Windows XP R 2.1.1 Patched (2005-09-19) you get:

Error: protect(): protection stack overflow

It seems that the R.2.2.0 revision you have does not protect against 
this.  I agree that it should not be possible to crash R, but is it 
valid to call NextMethod() in a default function? [R core, should this 
ever be allowed?]

I do not know exactly how NextMethod() is expected to work here, but I 
could imaging that 'x' has class 'Date' when NextMethod() is called and 
the "next" class will the be the default one so you call 
julian.default() again ending up in an infinite call.  This makes sense 
from the errors I get above.  Try this and see what you get in your version:

julian.default <- function(x, ...) {
   cat("In julian.default()\n")
   x <- as.Date(x)
   NextMethod("julian", x, ...)
}

Was you intention to do the following instead

  julian.default <- function(x, ...) {
    x <- as.Date(x)
    julian(x, ...)
  }

where julian() is the generic function?

Cheers

Henrik


> Here is a test example 
> 
> 
>>m <- as.Date("1972-09-27") + 0:10
>>m 
> 
>  [1] "1972-09-27" "1972-09-28" "1972-09-29" "1972-09-30" "1972-10-01"
> "1972-10-02" "1972-10-03"
>  [8] "1972-10-04" "1972-10-05" "1972-10-06" "1972-10-07"
> 
>>class(m)
> 
> [1] "Date"
> 
>>julian(m)
> 
>  [1] 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
> attr(,"origin")
> [1] "1970-01-01"
> 
> 
>>m <- as.character(m)
>>class(m)
> 
> [1] "character"
> 
> 
>>julian(m)
> 
> 
> < R crashes>
> 
> --please do not edit the information below--
> 
> Version:
>  platform = i386-pc-mingw32
>  arch = i386
>  os = mingw32
>  system = i386, mingw32
>  status = 
>  major = 2
>  minor = 2.0
>  year = 2005
>  month = 10
>  day = 06
>  svn rev = 35749
>  language = R
> 
> 
> 
> Bill Venables, 
> CMIS, CSIRO Laboratories, 
> PO Box 120, Cleveland, Qld. 4163 
> AUSTRALIA 
> Office Phone (email preferred): +61 7 3826 7251 
> Fax (if absolutely necessary):    +61 7 3826 7304 
> Mobile (rarely used):                +61 4 1963 4642 
> Home Phone:                          +61 7 3286 7700 
> mailto:Bill.Venables at csiro.au 
> http://www.cmis.csiro.au/bill.venables/
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
> 
>



More information about the R-devel mailing list