Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protect the function wrapped by cpp11::function #395

Merged
merged 3 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# cpp11 (development version)

* `cpp11::function` now protects its underlying function, for maximum safety
(#294).

* `cpp11::writable::r_vector<T>::proxy` now implements copy assignment.
Practically this means that `x[i] = y[i]` now works when both `x` and `y`
are writable vectors (#300, #339).
Expand Down
43 changes: 36 additions & 7 deletions inst/include/cpp11/function.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class function {
}

private:
SEXP data_;
sexp data_;

template <typename... Args>
void construct_call(SEXP val, const named_arg& arg, Args&&... args) const {
Expand Down Expand Up @@ -71,36 +71,65 @@ class package {
return safe[Rf_findVarInFrame](R_NamespaceRegistry, name_sexp);
}

// Either base env or in namespace registry, so no protection needed
SEXP data_;
};

namespace detail {

// Special internal way to call `base::message()`
//
// - Pure C, so call with `safe[]`
// - Holds a `static SEXP` for the `base::message` function protected with
// `R_PreserveObject()`
//
// We don't use a `static cpp11::function` because that will infinitely retain a cell in
// our preserve list, which can throw off our counts in the preserve list tests.
inline void r_message(const char* x) {
static SEXP fn = NULL;

if (fn == NULL) {
fn = Rf_findFun(Rf_install("message"), R_BaseEnv);
R_PreserveObject(fn);
}

SEXP x_char = PROTECT(Rf_mkCharCE(x, CE_UTF8));
SEXP x_string = PROTECT(Rf_ScalarString(x_char));

SEXP call = PROTECT(Rf_lang2(fn, x_string));

Rf_eval(call, R_GlobalEnv);

UNPROTECT(3);
}

} // namespace detail

inline void message(const char* fmt_arg) {
static auto R_message = cpp11::package("base")["message"];
#ifdef CPP11_USE_FMT
std::string msg = fmt::format(fmt_arg);
R_message(msg.c_str());
safe[detail::r_message](msg.c_str());
#else
char buff[1024];
int msg;
msg = std::snprintf(buff, 1024, "%s", fmt_arg);
if (msg >= 0 && msg < 1024) {
R_message(buff);
safe[detail::r_message](buff);
}
#endif
}

template <typename... Args>
void message(const char* fmt_arg, Args... args) {
static auto R_message = cpp11::package("base")["message"];
#ifdef CPP11_USE_FMT
std::string msg = fmt::format(fmt_arg, args...);
R_message(msg.c_str());
safe[detail::r_message](msg.c_str());
#else
char buff[1024];
int msg;
msg = std::snprintf(buff, 1024, fmt_arg, args...);
if (msg >= 0 && msg < 1024) {
R_message(buff);
safe[detail::r_message](buff);
}
#endif
}
Expand Down
Loading