filesystem::path crash on Windows when dealing with umlauts ( üëï )
I am getting a crash on Windows when passing a path that contains umlauts (like sampëüö ) to the path constructor. I tried boost 1.69 and boost 1.60. Here's the code I am using: namespace fs = boost::filesystem; const char* tmp = setlocale(LC_ALL, N_("")); // Create and install global locale std::locale::global(boost::locale::generator().generate( N_("") )); // Make boost.filesystem use it fs::path::imbue(std::locale()); if ( !tmp ) tmp = setlocale( LC_ALL, NULL ); std::string program = argv[0]; fs::path file = fs::path( program ); // CRASH -- Gonzalo Garramuño
On 08.05.19 14:35, Gonzalo Garramuño via Boost-users wrote:
I am getting a crash on Windows when passing a path that contains umlauts (like sampëüö ) to the path constructor.
I tried boost 1.69 and boost 1.60.
Here's the code I am using:
namespace fs = boost::filesystem;
const char* tmp = setlocale(LC_ALL, N_(""));
// Create and install global locale std::locale::global(boost::locale::generator().generate( N_("") ));
// Make boost.filesystem use it fs::path::imbue(std::locale());
if ( !tmp ) tmp = setlocale( LC_ALL, NULL );
std::string program = argv[0]; fs::path file = fs::path( program ); // CRASH
This has nothing to do with boost::filesystem, but... Don't use argv[0] on Windows, and generally don't use argv for path arguments on Windows. Any Windows program that uses argv[0] for any purpose other than diagnostic output is incorrect. The reason is, of course, that Windows transforms unicode paths into the local codepage in order to generate argv, which is a lossy operation that can cause non-ASCII characters to be replaced with other characters or to be dropped entirely. You might think that this doesn't matter to you because you run in a locale that can represent all of the characters in your path. However, I can guarantee that this will not hold true for all of your users. Use GetCommandLineW to get the actual Unicode command line, CommandLineToArgvW to convert it to UTF-16, then convert from that to UTF-8 or some other sane representation. Or just pass the wchar string directly to boost::filesystem. argv[0] is broken on Windows. Do not use it. -- Rainer Deyke - rainerd@eldwood.com
On 9/05/2019 00:58, Rainer Deyke wrote:
Don't use argv[0] on Windows, and generally don't use argv for path arguments on Windows. Any Windows program that uses argv[0] for any purpose other than diagnostic output is incorrect.
You can use argv for path arguments, but only if you're using wmain or wWinMain entry points and using the wchar overloads of Filesystem. main or WinMain entry points receive ANSI-codepage arguments, which are guaranteed to not be UTF-8 and are never safe to use as filesystem paths.
argv[0] is broken on Windows. Do not use it.
argv[0] is broken everywhere; it can be set fairly arbitrarily by whoever launches the process, on all platforms.
participants (3)
-
Gavin Lambert
-
Gonzalo Garramuño
-
Rainer Deyke