Skip to content

Keep letters outside ASCII in a Windows program's arguments - #516

Draft
dimensionscape wants to merge 1 commit into
mainfrom
fix/windows-wide-arguments
Draft

dimensionscape wants to merge 1 commit into
mainfrom
fix/windows-wide-arguments

Conversation

@dimensionscape

Copy link
Copy Markdown
Member

On Windows, Sys.args() in a console program loses letters outside ASCII. On a machine whose ANSI code page is 1252:

  • café arrives as caf and U+FFFD;
  • 日本 arrives as ??;
  • αβ arrives as a and U+FFFD;
  • the two arguments a, U+FF02 (a full width quote), space, b and c arrive as a and b c.

A script run by the cppia host, Cppia.exe, gets the same.

Cause

The C runtime splits the ANSI copy of the command line and hands main bytes of the ANSI code page. main stores them in _hxcpp_argv, and __get_args reads _hxcpp_argv as UTF-8. A lone byte above 0x7F is not UTF-8, hence U+FFFD. A letter the code page lacks was replaced before the program started, by ? or by the nearest character the code page has. For U+FF02 that is a quote, which the runtime then takes as one.

A program built with -D no_console on MSVC enters through WinMain, has no arguments of main's, and reads the wide command line already. The letters arrive there before and after this change.

The change

main passes its arguments through _hxcpp_windows_utf8_argv, in src/hx/StdLibs.cpp, before it stores them:

  • A command line of ASCII is returned as it is, the same pointers.
  • Where the ANSI command line converts back to exactly the wide one, nothing was lost. The arguments stay the runtime's and are written as UTF-8, so its splitting is kept, and its wildcard expansion in a program linked with setargv.obj.
  • Where it does not, the wide command line is split here, by the rules the C runtime documents ("Parsing C command-line arguments"). Nothing expands wildcards on that path, so a program linked with setargv.obj gets *.txt as it was typed when another argument holds a letter the code page lacks.

_hxcpp_argv stays an array of UTF-8 strings and __get_args is not changed, so a host that sets _hxcpp_argv itself is read as before, and the cppia host still moves it past its own options.

Tests

test/regression/ArgsWide starts itself with arguments and compares the code units Sys.args() gives the child with what was sent: ASCII, a letter of the code page, letters of three other scripts, a character above U+FFFF, such letters beside a space, a quote, a backslash and an empty argument, an argument that starts with #, and two characters whose nearest ANSI character is a quote and a space. On Windows it also writes six command lines out whole, for the splitting rules Process never produces: two quotes inside quotes, backslashes before a quote and before a letter, a tab, and a quote left open. Before the change 18 of its 19 lines are false.

CppiaHostArgs gets one more line, a script that sees café and 日本 after its name. It is false before the change.

Run on 64-bit Windows with MSVC and Haxe 4.3.7, ANSI code page 1252:

  • ArgsWide as a 64-bit and as a 32-bit program, and CppiaHostArgs.
  • 94 regression tests in all: those two, EnvironmentWide, ProgramPathNamesProgram, the 13 Process*, the 5 Utf8* and the 73 Cppia*. They ran before one comment in StdLibs.cpp was reworded, and ArgsWide and CppiaHostArgs ran again after.
  • The splitting against the C runtime's own, on 3,740 command lines of quotes, backslashes, tabs, spaces, #, * and letters, with the program's own name varied too. Each was sent once as ASCII, where the arguments are the runtime's, and once with a letter the code page lacks, where they are split here. None differed.
  • Four ways of breaking the change by hand each make ArgsWide fail: two quotes inside quotes no longer one quote, backslashes before a quote not halved, the runtime's arguments not written as UTF-8, and the wide command line never split.
  • The program run from folders named café dir and 日本, and from one named with an emoji: its arguments arrive.
  • A program linked with setargv.obj, in a folder that holds one.txt and two.txt. *.txt and *.txt café give both file names, as on main, and café now arrives. *.txt 日 gives *.txt and 日. On main it gave the two file names and no third argument.
  • ArgsWide built with -D no_console: 18 of its 19 lines are true on main and here, every line about letters among them. The false one is two quotes inside quotes: that entry has a splitter of its own, which this does not change.

Not run here: Linux, macOS, MinGW, another ANSI code page, the rest of test/regression, and the other suites.

The Changes.md entry sits at the same place as the one in #515, so whichever merges second needs a rebase.

On Windows the C runtime hands main its arguments in the ANSI code page,
and __get_args reads them as UTF-8. A letter of the code page, as the e
acute of code page 1252, came out of Sys.args() as U+FFFD, and a letter
the code page lacks was already a "?". For a character it lacks Windows
writes the nearest it has, so a full width quote (U+FF02) became a quote
and ended the argument: "a<U+FF02> b" and "c" arrived as "a" and "b c".

main now passes its arguments through _hxcpp_windows_utf8_argv before
anything reads them:

- a command line of ASCII is returned as it is;
- where the ANSI command line still holds every character, the
  arguments stay the runtime's and are written as UTF-8, so its
  splitting is kept, and its wildcard expansion in a program linked
  for that;
- where it does not, the wide command line is split here by the rules
  the runtime documents. Nothing expands wildcards there, so a program
  linked with setargv.obj gets none on such a command line.

_hxcpp_argv stays an array of UTF-8, so the cppia host still moves it
past its own options, and a script it runs gets the same arguments. The
WinMain entry of an MSVC build with -D no_console is unchanged: it has
no arguments of main's and reads the wide command line already, with a
splitter of its own.

ArgsWide starts itself with such arguments, through Process and, for
the splitting rules, as command lines written out whole. CppiaHostArgs
passes two to a script. The splitting was also compared with the C
runtime's own on 3,700 command lines of quotes, backslashes, tabs and
spaces, with no difference.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant