r-lib / cpp11

cpp11 helps you to interact with R objects using C++ code.
https://cpp11.r-lib.org/
Other
199 stars 46 forks source link

cpp11::preserved relies on ordered global construction #224

Closed bkietz closed 3 years ago

bkietz commented 3 years ago

cpp11::preserved relies on the list_ data member having a well-defined value at the time preserved is accessed. Since the order of construction of globals is not guaranteed in c++, that can lead to undefined behavior if you're constructing (for example) a global cpp11::strings since that appends to preserved in the constructor.

namespace {

// the value of `cpp11::preserved.list_` is not guaranteed here!
cpp11::strings kColorStrings = cpp11::writable::strings({
  "red",
  "green",
  "blue",
});

} // namespace