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

Rcpp::warning does not follow the correct interface for Rf_warning #776

Closed
jimhester opened this Issue Nov 22, 2017 · 3 comments

Comments

Projects
None yet
2 participants
@jimhester
Contributor

jimhester commented Nov 22, 2017

Rf_warning is defined with a function prototype like that of printf

void warning(const char *format, ...)

However Rcpp::warning() calls it instead with only a null terminated string (I linked here to the C11 code, but the C89 code has the same problem).

template <typename... Args>
inline void warning(const char* fmt, Args&&... args ) {
Rf_warning( tfm::format(fmt, std::forward<Args>(args)... ).c_str() );
}

This means that any values passed to Rcpp::warning() which happen to have format specifiers in them will access random memory, outputting garbage or potentially crashing R.

For a simple example illustrating the issue.

Rcpp::cppFunction('void test() { Rcpp::warning("%s", "a str with %o in it"); }')
test()
#> Warning in test(): a str with 1543134670 in it
@eddelbuettel

This comment has been minimized.

Member

eddelbuettel commented Nov 22, 2017

Ugggh.

What should we do? Refuse to process strings with %[a-z] in them? Pass them to tinyformat?

@jimhester

This comment has been minimized.

Contributor

jimhester commented Nov 22, 2017

I would either

  1. Remove tinyformat from the equation entirely for warnings and pass the arguments directly to Rf_warning().
  2. Always specify a first argument of %s and leave the tiny format the way it is as the second argument.

With either approach you shouldn't need to sanitize the inputs.

jimhester added a commit to r-lib/xml2 that referenced this issue Nov 22, 2017

Use Rf_warning instead of Rcpp::warning
Works around a bug in Rcpp RcppCore/Rcpp#776

Fixes #199
@eddelbuettel

This comment has been minimized.

Member

eddelbuettel commented Nov 25, 2017

Approach 2 is good. PR forthcoming.

eddelbuettel added a commit that referenced this issue Nov 26, 2017

Merge pull request #777 from RcppCore/bugfix/rcpp_warning
correct calls to Rf_warning (closes #776)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment