tabulate causes segmentation fault (PR#156)

Bill Venables wvenable@arcola.stats.adelaide.edu.au
Sun, 4 Apr 1999 09:41:39 +0930 (CST)


>>>>> "Peter" == Peter Dalgaard BSA <p.dalgaard@biostat.ku.dk> writes:

    Peter> wvenable@arcola.stats.adelaide.edu.au writes:

    >> R : Copyright 1999, The R Development Core Team Version
    >> 0.63.3 (March 6, 1999)
    >> 
    >> ....
    >> 
    >> [Previously saved workspace restored]
    >> 
    >> > tabulate(1:10, 5)
    >> 
    >> Process R:1 segmentation fault at Sat Apr 3 17:48:34 1999

    Peter> It wasn't...

    Peter> However, all it needs is a bit of defensive
    Peter> programming.

Yes, Peter, but by whom?  My point was that this kind of massive
bear trap was not something that should be the users' sole
responsibility to avoid.  Having a simple exception capable of
killing the session and losing all data is perhaps OK for
MicroSoft, but it should not be OK for us.  I believe the flaw is
still present in the nascent 0.64, which bothers me.

I think it should be solved at the C level since tabulate() is
one of those things that you want to be ultra-slick, but if this
is not possible right now, perhaps we will have to put up with a
stop-gap measure.  For example you could change it in 0.64 to

tabulate <- function (bin, nbins = max(bin)) {  
    if (!is.numeric(bin) && !is.factor(bin)) 
        stop("tabulate: bin must be numeric or a factor")

    nbins <- max(0, nbins) 
    if(!missing(nbins) && !all(OK <- (bin <= nbins))) 
	bin <- bin[OK]
    n <- length(bin)
    storage.mode(bin) <- "integer"

    .C("tabulate", bin, n, ans = integer(nbins))$ans
}

> tabulate(1:10, 5)
[1] 1 1 1 1 1
> tabulate(1:10 + 0.1, 5)
[1] 1 1 1 1 0
> tabulate(1:10, 0)
numeric(0)
> tabulate(sample(1:10, 50000, rep=T))
 [1] 5016 5048 4996 5020 4914 5089 5059 4885 4998 4975
> 

I think any degradation in performance over the present version
would be mostly negligible and the whole thing would be much safer.
Users who need ultra-slickness still have the option of using he
C code directly - that is the wonderful benefit of open code.

Bill
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._