Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shim doesn't handle spaces in arguments and executable path #10

Open
kodemeister opened this issue Aug 29, 2023 · 0 comments · May be fixed by #11
Open

Shim doesn't handle spaces in arguments and executable path #10

kodemeister opened this issue Aug 29, 2023 · 0 comments · May be fixed by #11

Comments

@kodemeister
Copy link

kodemeister commented Aug 29, 2023

Hey @aloneguid,

Thank you for this little gem! This is the only opensource alternative to Chocolatey shim generator I've managed to find!

While playing with the tool, I've faced several issues with spaces in arguments passed to the shim executable. They can be easily reproduced in the following way:

  1. Make a shim to the mkdir command:
shmake.exe -i cmd.exe -o sample.exe -a "/c mkdir %s"
  1. Try to create a folder containing spaces in its name:
sample.exe "my folder"

Expected behavior: shim creates a single folder named my folder
Actual behavior: shim creates two separate folders named my and folder

This is caused by the following code:

wstring passed_arg;
for (int i = 1; i < argc; i++)
{
    if (i > 1) passed_arg += L" ";
    passed_arg += argv[i];
}

Unfortunately we cannot simply concatenate all arguments into a single string. Any argument that contains spaces must be enclosed in double quotes, otherwise it would be unclear where the previous argument ends, and where the next one starts.

The easiest fix is to retrieve the entire command-line string from GetCommandLine and forward it to the target process. This is exactly what scoop-better-shimexe does. GetCommandLine returns the command-line string as-is, preserving the original quotation marks around arguments. No need to manually construct the string from separate argv tokens and mess with the awkward quoting rules. There is one caveat though. The command-line string always starts with the path to the shim executable, i.e., argv[0]. This path needs to be removed from the string before doing %s token substitution.

Note that spaces may occur not only in arguments but also in executable paths like C:\Program Files. For instance:

shmake.exe -i "C:\Program Files\Git\bin\git.exe" -o sample.exe

Honestly, I didn't have any issues with such paths but some Scoop people reported that Scoop shims cannot run executables if they have a space in their path. The fix is simple: if the executable path contains spaces, enclose it in double quotes. scoop-better-shimexe already did this. It would be good to apply similar fixes here as well.

mnivet added a commit to avanteam/win-shim that referenced this issue Oct 11, 2023
This fix to issue aloneguid#10 is better than the one we initially proposed.
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 a pull request may close this issue.

1 participant