Length of optimized c++ strings does not change when prefix is replaced #2524
Labels
locked
[bot] locked due to inactivity
stale::closed
[bot] closed after being marked as stale
stale
[bot] marked as stale due to inactivity
Consider a piece of C++11 code like:
where
DEFAULT_NNPDF_PROFILE_PATH
is some compile time constant that contains theconda
prefix, and we hope to have replaced for the corresponding environment prefix.This works, and the substitution indeed happens. However users end up seeing a path like:
with all the zeros. The reason is that an optimizing compiler may decide to build the
std::string
object at compile time and set its length to the length of the "placeholder" prefix, while not bothering to recompute it at runtime, from the position of the first \0. This then breaks in all sorts of ways (e.g. python raises an exception if \0s are passed as paths). The solution is to make a variable volatile:to force the compiler to skip the optimization for that code.
I don't know if there is a better way of doing this. In particular it requires patching third party code. See:
Zaharid/conda-recipes@c17240a
I have seen this problem with both gcc on linux and clang on mac, when sufficiently recent versions are used.
The text was updated successfully, but these errors were encountered: