From p.dalgaard@biostat.ku.dk  Sat Apr  1 19:31:42 2000
From: p.dalgaard@biostat.ku.dk (Peter Dalgaard BSA)
Date: 01 Apr 2000 20:31:42 +0200
Subject: [Rd] Bug ? mine or ? in R core
In-Reply-To: Michael Lapsley's message of "Sat, 1 Apr 2000 19:18:09 +0100"
References: <20000401191809.A16446@athome.lapsley>
Message-ID: <x2bt3tsmep.fsf@blueberry.kubism.ku.dk>

Michael Lapsley <mlapsley@ndirect.co.uk> writes:

[Crashes with the following code]

> PROTECT(ans=NEW_LIST(2)); /*create answer [0] = data, [1]=stat */
> PROTECT(stat=NEW_INTEGER(1)); /* numeric status vector */
> PROTECT(data=NEW_CHARACTER(buffsize));
> while(1){
> 	for (i=1; i<= NCOLS; i++){
> 			offset=((i-1)*maximum)+(j-1);
> 		}
> 	  DBG(2,"writing to data");
> 	  STRING(data)[offset]=COPY_TO_USER_STRING("Some dummy data");
> 		if(offset +NCOLS >= buffsize){
> 			DBG(1,"reallocating space");
> 			length=length(data);
> 			buffsize=length+buffsize;
> 			SET_LENGTH(data,buffsize);
                        ^^^^^^^^^^^^^^^^^^^^^^^^^
> 		}
> 	j++;
> 	if(maximum && j>maximum)
> 		break;
> }	
> 
> /* transpose it */
> DBG(1,"trimming");
> SET_LENGTH(data,j*NCOLS); /* trim down buffer*/
  ^^^^^^^^^^^^^^^^^^^^^^^^

I'm not too well accustomed to these macros (they aren't used in the R
internals), but in Rdefines.h we have

#define SET_LENGTH(x, n)       (x = lengthgets(x, n))

so I wouldn't count on "data" being the same after those operations,
and in particular not expect it to be protected anymore. You probably
need to stick in "UNPROTECT(1); PROTECT(data);" after the SET_LENGTH
operations.

-- 
   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] Bug ? mine or ? in R core
Peter Dalgaard BSA