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

warning when compiling with clang-10 on macOS #1066

Closed
kevinushey opened this issue Apr 10, 2020 · 1 comment · Fixed by #1067
Closed

warning when compiling with clang-10 on macOS #1066

kevinushey opened this issue Apr 10, 2020 · 1 comment · Fixed by #1067

Comments

@kevinushey
Copy link
Contributor

kevinushey commented Apr 10, 2020

Saw this while compiling Rcpp with the latest version of LLVM on macOS:

/usr/local/opt/llvm/bin/clang++ -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -fsanitize=address,undefined -fno-omit-frame-pointer -fno-sanitize=alignment,function,float-divide-by-zero -std=gnu++11 -I"/Users/kevinushey/r/r-devel-sanitizers/lib/R/include" -DNDEBUG -I../inst/include/  -I/usr/local/include   -fPIC  -g -O1 -Wall -pedantic -DSWITCH_TO_REFCNT  -c module.cpp -o module.o
api.cpp:136:12: warning: address of stack memory associated with local variable 'f' returned [-Wreturn-stack-address]
    return f.c_str();
           ^
1 warning generated.

The offending routine is here:

Rcpp/src/api.cpp

Lines 129 to 137 in c9cf6aa

// [[Rcpp::register]]
const char* short_file_name(const char* file) { // #nocov start
std::string f(file);
size_t index = f.find("/include/");
if (index != std::string::npos) {
f = f.substr(index + 9);
}
return f.c_str();
}

We are indeed returning a pointer to the data for a std::string on the stack, which is unsafe. Note that while this is a registered routine, it's only used internally (when Rcpp debugging is enabled via RCPP_DEBUG). Either way, two potential fixes here would be:

  1. Just return a std::string rather than a const char*, or
  2. Use a static std::string, and return a pointer to the data for that std::string.
@eddelbuettel
Copy link
Member

Ugg. Option 1. is probably safer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants