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

Douglas Bates bates at stat.wisc.edu
Mon Oct 8 17:21:45 CEST 2012


On Mon, Oct 8, 2012 at 6:42 AM, Dirk Eddelbuettel <edd at debian.org> wrote:
>
> Hi Ben,
>
> On 8 October 2012 at 10:24, North, Ben wrote:
> | 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 :-)
>
> It's been a while since we had something like this, and Doug lead it the last
> time. From what I recall, Rf_install() is safe, and the reason we factor
> these out as below. The bad boy, as I call, is probably Rf_mkString so by just doing
>
>  str_what  = Rf_install(exception_what.c_str());
>  str_class = Rf_install(exception_class.c_str());
>  str_ns    = Rf_install("Rcpp"));
>  Rf_eval(Rf_lang3(cppExceptSym, str_what, str_class), str_ns);
>
> may be enough.  Could you test that?

Just glancing quickly at this, I think it may or may not work.
Rf_install looks up a symbol in the symbol table, installing it if
needed, and returns the symbol SEXP.  Rf_mkstring creates a string
SEXP (vector of R class "character" containing a single string).  The
string probably needs protection.  The symbol does not.

I imagine the R function cpp_exception expects strings.  I'm not sure
what happens if it gets symbols.

> |
> | 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.
> | _______________________________________________
> | Rcpp-devel mailing list
> | Rcpp-devel at lists.r-forge.r-project.org
> | https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>
> --
> Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel


More information about the Rcpp-devel mailing list