[Rd] grep and PCRE fun

From: Jeffrey Horner <jeffrey.horner_at_gmail.com>
Date: Thu, 29 Sep 2011 16:00:42 -0500


I think I've found a bug in the C function do_grep located in src/main/grep.c. It seems to affect both the latest revisions of R-2-13-branch and trunk when compiling R without optimizations and with it's own version of pcre located in src/extra, at least on ubuntu 10.04.

 According to the pcre_exec API (I presume the later versions), the ovecsize argument must be a multiple of 3 , and the ovector argument must point to a location that can hold at least ovecsize integers. All the pcre_exec calls made by do_grep, save one, honors this. That one call seems to overwrite areas of the stack it shouldn't. Here's the smallest example I found that tickles the bug:

> grep("[^[:blank][:cntrl]]","\\n",perl=TRUE)
Error in grep("[^[:blank][:cntrl]]", "\\n", perl = TRUE) :   negative length vectors are not allowed

As described above, this error occurs on ubuntu 10.04 when R is compiled without optimizations ( I typically use CFLAGS="-ggdb" CXXFLAGS="-ggdb" FFLAGS="-ggdb" ./configure --enable-R-shlib), and the pcre_exec call executed from do_get overwrites the integer nmatches and sets it to -1. This has the effect of making do_grep try and allocate a results vector of length -1, which of course causes the error message above.

I'd be interested to know if this bug happens on other platforms.

Below is my simple fix for R-2-13-branch (a similar fix works for trunk as well).


$ svn diff main/grep.c
Index: main/grep.c

R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Thu 29 Sep 2011 - 21:09:10 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 30 Sep 2011 - 15:15:39 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive