Skip to content

Commit

Permalink
Fix locale, charset and filesystem path related issue
Browse files Browse the repository at this point in the history
Using the "C" locale as default does not cover more "exotic" paths, which can result in exceptions thrown by boost::filesystem::path.

boost::filesystem::path::imbue() returns the "previously used locale", which should be the "right one", if called in a pristine environment, or to put it another way: if imbue() wouldn't be called at all, then this appears to be the one boost::filesystem::path would use.

In this patch, a dummy locale is used to extract the internal locale, to use it explicitly, which seems to be a preferable choice, because the goal is not to modify the locale at all, but to prevent deinitialization issues, and the bad-environment-fallback also kicks in earlier.
  • Loading branch information
dexX7 committed May 1, 2015
1 parent d8ac901 commit c9a3784
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -713,18 +713,20 @@ void RenameThread(const char* name)

void SetupEnvironment()
{
std::locale loc("C");
// On most POSIX systems (e.g. Linux, but not BSD) the environment's locale
// may be invalid, in which case the "C" locale is used as fallback.
#if !defined(WIN32) && !defined(MAC_OSX) && !defined(__FreeBSD__) && !defined(__OpenBSD__)
try {
loc = std::locale(""); // Raises a runtime error if current locale is invalid
std::locale(""); // Raises a runtime error if current locale is invalid
} catch (const std::runtime_error&) {
setenv("LC_ALL", "C", 1);
}
#endif
// The path locale is lazy initialized and to avoid deinitialization errors
// in multithreading environments, it is set explicitly by the main thread.
// The path locale is lazy initialized and to avoid deinitialization errors in
// multithreading environments, a dummy locale is used to extract the internal
// default locale of boost::filesystem::path, which is then used explicitly by
// the main thread.
std::locale loc = boost::filesystem::path::imbue(std::locale::classic());
boost::filesystem::path::imbue(loc);
}

Expand Down

0 comments on commit c9a3784

Please sign in to comment.