[Rcpp-devel] lack of protection in Nullable operator=(SEXP) ?

Serguei Sokol serguei.sokol at gmail.com
Tue Jun 4 18:35:22 CEST 2019


Hi,

I came across a case when under gctorture, a nullable variable set to an 
integer changed unwillingly behind the stage to a character. It looks 
like the previously set integer value was not protected so was free-ed 
by gc() and received a newly created character value.
Here is a small reproducible example. Potential culprit of lacking 
protection is the line 'x=wrap(10);' The error shows up at 'as<int>(x)'.

// file nullable.cpp
#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
int add1(int x) {
   return x+1;
}
// [[Rcpp::export]]
SEXP add1_nullable(Nullable<int> x) {
   if (x.isNull())
     x=wrap(10);
   // create few r objects to make gctorture() to show up
   List l=List::create(_("a")="aha", _("o")="oho", _("b")="boo");
   return wrap(add1(as<int>(x)));
}
/*** R
gctorture(on=TRUE)
x=2
add1(x)
add1_nullable(x)
add1_nullable(NULL)
gctorture(on=FALSE)
*/
// end of file nullable.cpp

In R:

library(Rcpp)
sourceCpp("nullable.cpp")

# output that I have:

 > gctorture(on=TRUE)

 > x=2

 > add1(x)
[1] 3

 > add1_nullable(x)
[1] 3

 > add1_nullable(NULL)
Error in add1_nullable(NULL) :
   Not compatible with requested type: [type=character; target=integer].

Am I doing something wrong or Nullable operator=(SEXP) should be corrected?

Best,
Serguei.

Linux
R version 3.6.0
 > packageVersion("Rcpp")
[1] ‘1.0.1’



More information about the Rcpp-devel mailing list