Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-26303

Qt's command-line tokenizer (used e.g. by the qtmain.lib) handles escape characters wrong!

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P3: Somewhat important
    • None
    • 4.8.0, 4.8.1, 4.8.2
    • Core: Other
    • None

    Description

      Qt's command-line tokenizer (used e.g. by the qtmain.lib) handles escape characters wrong!

      The problem is in the function: qWinCmdLine(), located at qcorecmdlineargs_p.h

      That function is used to emulate how the command-line string is tokenized into parameters, as required for argv[].
      It is used (not only) in the qtmain.lib to call the programs actualy main() function.

      The bug is as follows:
      The backslash () is handled as an escape character by Qt when it appears right before a (") or a (') character.
      The former is correct, so we can have literal (") characters on the command-line. But it is wrong for (') characters!

      Note that the (') character does a special meaning in C source code, but not on the command-line.

      Example:
      Here is an example for a command-line that Qt does handle WRONG at the moment:
      program.exe --add "C:\Test\''Foo''.bar"

      The correct and expected result would be:
      argv(0) = program.exe
      argv(1) = --add
      argv(2) = C:\Test\''Foo''.bar

      But what the Qt command-line tokenizer currently returns is the following:
      argv(0) = program.exe
      argv(1) = --add
      argv(2) = C:\Test''Foo''.bar

      As you see, the backslash has disappeared. This screws up the path, making it impossible to open the file.
      It's obviously because Qt handled (\') as an escape sequence for ('), but this is not only superfluous but also wrong!

      To make this clear again, when the C++ Runtime calls main() directly, i.e. the command-line is tokenized by the C++ Runtime instead of Qt, then we do get the correct/expected result (as shown above). I have confirmed this on Windows (MSVC) as well as on Linux (GCC). Also when we use GetCommandLineW() plus CommandLineToArgvW() via Win32 API, we do get the correct result. Only Qt gives a different and wrong result!

      BTW: As qWinCmdLine() seems to be Windows-only anyway, I would suggest to replace that function with a simple CommandLineToArgvW() wrapper.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            Unassigned Unassigned
            mulder LoRd MuldeR
            Votes:
            2 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There are no open Gerrit changes