Fix R memory leak on exception

There is a possible memory leak in case the SWIG_exception_fail macro
is called. The problem is related to its definition that call the
function Rf_warning. This function (as well as Rf_error) involves
a longjmp over C++ destructors on the stack. Thus, all the objects
allocated on the heap are not freed.

Closes #914
This commit is contained in:
Arnaud Barré 2020-01-30 19:52:53 +00:00 • committed by William S Fulton
commit 3fa5c8c652
6 changed files with 129 additions and 8 deletions

View file

@ -1,7 +1,3 @@
#define SWIG_Error(code, msg) Rf_warning(msg); return Rf_ScalarLogical(NA_LOGICAL)
#define SWIG_fail return Rf_ScalarLogical(NA_LOGICAL)
/* for raw pointers */
#define SWIG_ConvertPtr(oc, ptr, ty, flags) SWIG_R_ConvertPtr(oc, ptr, ty, flags)
#define SWIG_ConvertFunctionPtr(oc, ptr, ty) SWIG_R_ConvertPtr(oc, ptr, ty, 0)

View file

@ -1,3 +1,46 @@
#include <stdarg.h> // va_list, va_start, va_end
#include <stdio.h> // vsnprintf
/* Last error */
static int SWIG_lasterror_code = 0;
static char SWIG_lasterror_msg[1024];
SWIGRUNTIME void SWIG_Error(int code, const char *format, ...) {
va_list arg;
SWIG_lasterror_code = code;
va_start(arg, format);
vsnprintf(SWIG_lasterror_msg, sizeof(SWIG_lasterror_msg), format, arg);
va_end(arg);
}
SWIGRUNTIME const char* SWIG_ErrorType(int code) {
switch (code) {
case SWIG_MemoryError:
return "SWIG:MemoryError";
case SWIG_IOError:
return "SWIG:IOError";
case SWIG_RuntimeError:
return "SWIG:RuntimeError";
case SWIG_IndexError:
return "SWIG:IndexError";
case SWIG_TypeError:
return "SWIG:TypeError";
case SWIG_DivisionByZero:
return "SWIG:DivisionByZero";
case SWIG_OverflowError:
return "SWIG:OverflowError";
case SWIG_SyntaxError:
return "SWIG:SyntaxError";
case SWIG_ValueError:
return "SWIG:ValueError";
case SWIG_SystemError:
return "SWIG:SystemError";
case SWIG_AttributeError:
return "SWIG:AttributeError";
}
return "SWIG:UnknownError";
}
#define SWIG_fail goto fail
/* Remove global namespace pollution */
#if !defined(SWIG_NO_R_NO_REMAP)