Bill.Venables@csiro.au
2005-Dec-21 03:32 UTC
[Rd] NextMethod causes R 2.2.0 to crash (PR#8416)
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, ...) } 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/
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 > >
Bill.Venables@csiro.au
2005-Dec-21 05:06 UTC
[Rd] NextMethod causes R 2.2.0 to crash (PR#8416)
Thanks Hendrik. My main concern is that it should not be so easy to crash R. On whether or not NextMethod from within the default method is kosher or not - I would have thought so, but clearly I'm wrong here. Perhaps NextMethod within a default method should be something that at least attracts a warning. Regards, 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/ -----Original Message----- From: Henrik Bengtsson [mailto:hb at maths.lth.se] Sent: Wednesday, 21 December 2005 1:48 PM To: Venables, Bill (CMIS, Cleveland) Cc: r-devel at stat.math.ethz.ch; R-bugs at biostat.ku.dk Subject: Re: [Rd] NextMethod causes R 2.2.0 to crash (PR#8416) 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 > >
ripley@stats.ox.ac.uk
2005-Dec-21 09:48 UTC
[Rd] NextMethod causes R 2.2.0 to crash (PR#8416)
Bill, This is a 2.2.0 Windows problem, solved in 2.2.1. What happened was that someone trying to be helpful increased the evaluation nesting limit in 2.1.0, and Windows has a small C stack (2Mb) which can easily be exceeded. I now get> julian(m)Error: evaluation nested too deeply: infinite recursion / options(expressions=)? as I should. The nesting limit has been reduced and the C stack size increased. It is legitimate to call NextMethod in a default method, but it is all too easy to get loops. In your case you want the Date method, not the next method (which since there is only one class will be the default). I would advise Windows users of 2.2.0 to 1) update 2) if that is not possible, set options(expressions=1000). Brian On Wed, 21 Dec 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, ...) > } > > 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 > >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595