Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#1371 SWIG R bindings don't check for NULL resulting in R crashing to desktop

open
nobody
None
5
2014-05-26
2014-05-26
Frank Bergmann
No

The code generated by SWIG does not check whether S4 elements passed in are valid elements (or contain externalptr's to NULL).

Example: assume we have a class Bar containing 10 instances of Foo, and is asked to return the eleventh. What happens is that we get an S4 object of type _p_Foo, whose external ptr is pointing to NULL.

Now suppose the Foo class had a function string getName(), if we now call the Foo_getName(x) with x the element from before, the R environment is crashing, as the external ptr is not checked.

Unfortunately my R knowledge is quite limited, however as workaround i found it working to change the default typemap in rtype.swg from:

 %typemap(scoercein) SWIGTYPE, SWIGTYPE *, SWIGTYPE *const, SWIGTYPE &  
 %{ 
     if (inherits($input, "ExternalReference")) $input = slot($input,"ref")
 %}

to

%typemap(scoercein) SWIGTYPE, SWIGTYPE *, SWIGTYPE *const, SWIGTYPE &  
%{ 
     if(isS4($input) && .hasSlot($input, "ref"))
     {
      if (length(grep("0x0",capture.output(slot($input, "ref")))) > 0)
      { 
        stop("Input null");
      }
     }
     if (inherits($input, "ExternalReference")) $input = slot($input,"ref")
 %}

Discussion