Boost logo

Boost Users :

Subject: Re: [Boost-users] filesystem::path crash on Windows when dealing with umlauts ( üëï )
From: Rainer Deyke (rainerd_at_[hidden])
Date: 2019-05-08 12:58:56


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_at_[hidden]

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net