sheredom / subprocess.h

🐜 single header process launching solution for C and C++
The Unlicense
1.1k stars 97 forks source link

Do not execute `commandLineCombined` when we have only one command parameter #60

Closed windowsair closed 2 years ago

windowsair commented 2 years ago

First of all, thank you for this project, it's the most convenient C/C++ library I've seen so far!

This question is out of my personal preference.

Sometimes I like to use a cmd like this:

const char* command_line[] = {
"foo \"bar\" \"\\\\\"\" ",
NULL
};

And this is the current msvc implementation in the project:

  // Combine commandLine together into a single string
  len = 0;
  for (i = 0; commandLine[i]; i++) {
    // For the ' ' and two '"' between items and trailing '\0'
    len += 3;

    for (j = 0; '\0' != commandLine[i][j]; j++) {
      switch (commandLine[i][j]) {
      default:
        break;
      case '\\':
        if (commandLine[i][j + 1] == '"') {
          len++;
        }

        break;
      case '"':
        len++;
        break;
      }
      len++;
    }
  }

Well, I didn't look closely at the code, but when I had only one command, I observed the wrong escape.

As mentioned above, this is likely to be a complex command line if there is only one command. It is better to follow the user's input and just take the command line without modifying it.

What do you think about this? Thanks!

windowsair commented 2 years ago

But it doesn't look like it meets the specification.... I'm not sure that's a good idea.

sheredom commented 2 years ago

Yeah I'm torn about this - it makes the library behave differently depending on if you have a single arg or not. Seems bad to me on first instincts!

windowsair commented 2 years ago

Yes, this approach may not be good. Perhaps we should focus on issues like #59.

takase1121 commented 2 years ago

The way libuv (and by extension Node.js) fixes this is by adding another option windowsVerbatimArgument which will simply concatenate the arguments without escaping.

https://nodejs.org/api/child_process.html#child_processexecfilefile-args-options-callback