[Rcpp-devel] Missing PROTECT()s in forward_exception_to_r()?

North, Ben Ben.North at sig.com
Mon Oct 8 12:24:51 CEST 2012


Hi,

I've been using Rcpp for a while now, and finding it very useful ---
thanks!

Recently, though, I came across strange behaviour when running under
gctorture(TRUE) and C++ exceptions were being forwarded to R.  The error
message string at the R level was some essentially random pathname, and
the string in the C++ exception was nowhere to be seen.  The classname
at the R level was the logical TRUE value.

The patch below fixed the problem for me.  I would not claim to fully
know what I'm doing, so perhaps somebody who does could review and then
make the proper fix :-)

Thanks,

Ben.

--- src/exceptions.cpp   2012-10-01 01:29:53.000000000 +0100
+++ ../Rcpp/src/exceptions.cpp     2012-10-08 11:14:39.435644100 +0100
@@ -161,14 +161,23 @@
               exception_class = name ; /* just using the mangled name */
          }
     }
-    SEXP cppExceptSym = Rf_install("cpp_exception"); // cannot cause a gc() once in symbol table
-    Rf_eval(
-        Rf_lang3(
-         cppExceptSym,
-              Rf_mkString(exception_what.c_str()),
-              Rf_mkString(exception_class.c_str())
-         ), R_FindNamespace(Rf_mkString("Rcpp"))
-    ) ;
+
+    SEXP cppExceptSym;
+    SEXP str_what, str_class, str_ns, ns, expr;
+
+    PROTECT(cppExceptSym = Rf_install("cpp_exception"));
+    PROTECT(str_what = Rf_mkString(exception_what.c_str()));
+    PROTECT(str_class = Rf_mkString(exception_class.c_str()));
+    PROTECT(str_ns = Rf_mkString("Rcpp"));
+
+    PROTECT(ns = R_FindNamespace(str_ns));
+    PROTECT(expr = Rf_lang3(cppExceptSym, str_what, str_class));
+
+    Rf_eval(expr, ns); /* Should not return. */
+
+    // Do this in case somehow someone replaces the definition of
+    // "cpp_exception" such that it does return:
+    UNPROTECT(6);
 }
 #else

________________________________

Susquehanna International Group Limited

Susquehanna International Group Limited is a private company limited by shares and registered in Ireland. Registration No.: 445356. Registered Address: 4th Floor, Georges Dock House, IFSC, Dublin 1, Ireland.
Susquehanna Ireland Limited is a private company limited by shares and registered in Ireland. Registration No.: 305632. Registered Address: 4th Floor, Georges Dock House, IFSC, Dublin 1, Ireland.
Susquehanna International Securities Limited is a private company limited by shares and registered in Ireland. Registration No.: 337946. Registered Address: 4th Floor, Georges Dock House, IFSC, Dublin 1, Ireland.

IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.


More information about the Rcpp-devel mailing list