R patch for strsplit

Jean Meloche jean@stat.ubc.ca
Fri, 02 Apr 1999 22:01:21 +0000


This is a multi-part message in MIME format.
--------------031DBBD5368F0FEAFF79DE0C
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

R core dumps when strsplit-ing long strings:

> str<-''; for(i in c(0:1000)) str<-paste(str,'a')
> strsplit(str,' ')
Bus Error (core dumped)

The attached patch fixes the problem in strsplit
by mallocating a short lived buffer of the right
size.

-- 
Jean Meloche
--------------031DBBD5368F0FEAFF79DE0C
Content-Type: text/plain; charset=us-ascii;
 name="ch.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="ch.diff"

--- character.c	Sun Feb 21 23:59:40 1999
+++ /usr/local/R-0.63.3/src/main/character.c	Fri Apr  2 13:24:03 1999
@@ -107,7 +107,7 @@
 {
     SEXP s, t, tok, x;
     int i, j, len, tlen, ntok;
-    char buff[MAXELTSIZE], *pt, *split = "";
+    char *buff, *pt, *split = "";
 
     checkArity(op, args);
     x = CAR(args);
@@ -119,6 +119,7 @@
     PROTECT(s = allocVector(VECSXP, len));
     for (i = 0; i < len; i++) {
 	/* find out how many splits there will be */
+	buff=malloc(strlen(CHAR(STRING(x)[i]))+1);
 	strcpy(buff, CHAR(STRING(x)[i]));
 	if (tlen > 0) {
 	    split = CHAR(STRING(tok)[i % tlen]);
@@ -150,6 +151,7 @@
 	}
 	UNPROTECT(1);
 	VECTOR(s)[i] = t;
+	free(buff);
     }
     UNPROTECT(1);
     return s;

--------------031DBBD5368F0FEAFF79DE0C--

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