llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Verbose mode unicode archive name printing is incorrect

Open chrisjbris opened this issue 8 years ago • 4 comments

Bugzilla Link 38669
Resolution FIXED
Resolved on Sep 04, 2018 17:11
Version trunk
OS Windows NT
Attachments example object files in input folder
CC @efriedma-quic,@rnk

Extended Description

When using the verbose modifier and creating an archive with a name containing Unicode characters, verbose printed archive name is mangled. However, the name of the created archive on the filesystem is correct. This issue was observed on Windows 10 using Git Bash and could not be reproduced on Ubuntu. For example,

On Windows 10: $ llvm-ar -rv llvm-ar☢☣.arc ./input/a.o ./input/b.o llvm-ar.exe: creating llvm-arÔÿóÔÿú.arc

On Ubuntu 16.04: $ llvm-ar rv llvm-ar☢☣.arc ./input/a.o ./input/b.o llvm-ar: creating llvm-ar☢☣.arc

chrisjbris avatar Aug 22 '18 11:08 chrisjbris

Thanks, fixed in r341433 with WriteConsoleW.

rnk avatar Sep 05 '18 00:09 rnk

I was thinking of something more along the lines of using GetConsoleScreenBufferInfo to detect whether stdout/stderr is redirected; if it isn't, use WriteConsoleW instead of write(). (We can assume any output written to a console is text, not binary.)

If stdout/stderr is redirected, it's not clear what the expected output should be... I think it's probably okay to just write UTF-8, though. I mean, the only other alternatives aren't great (either UTF-16 or the ANSI character set).

efriedma-quic avatar Aug 23 '18 18:08 efriedma-quic

llvm/llvm-project#35615 is related.

It's difficult to fix this because llvm::errs() and llvm::outs() use raw_fd_ostream, which is used to write both binary data and text.

We already have flags to indicate when we are outputting text or binary, but we currently go out of our way to set outs() to binary mode, probably so that we can write object files to stdout:

// Handle "-" as stdout. Note that when we do this, we consider ourself // the owner of stdout and may set the "binary" flag globally based on Flags. if (Filename == "-") { EC = std::error_code(); // If user requested binary then put stdout into binary mode if // possible. if (!(Flags & sys::fs::OF_Text)) sys::ChangeStdoutToBinary(); return STDOUT_FILENO; }

Fixing this would involve migrating tools away from outs() to some new outs_binary() that would have this effect, so that users of outs() that are writing text could get the transcoding behavior.

rnk avatar Aug 23 '18 17:08 rnk

Internally, LLVM represents all text as UTF-8, and translates to/from UTF-16 when calling Windows APIs.

Apparently, we're missing a spot where translation is necessary; instead of converting the UTF-8 text to UTF-16, llvm::errs() uses llvm::raw_fd_ostream, which calls the C library write(), which calls WriteConsoleA, which interprets the text as Windows-1252.

efriedma-quic avatar Aug 22 '18 22:08 efriedma-quic