Skip to content

Handle spaces in start_process arguments on Windows#494

Merged
ahcorde merged 2 commits intorollingfrom
cottsay/windows-argument-quotes
Apr 1, 2025
Merged

Handle spaces in start_process arguments on Windows#494
ahcorde merged 2 commits intorollingfrom
cottsay/windows-argument-quotes

Conversation

@cottsay
Copy link
Copy Markdown
Member

@cottsay cottsay commented Apr 1, 2025

The logic upon which this algorithm is based is derived from recommended practices on an MSDN blog.

As it stands, quotes on arguments are lost when the argument array is converted to a single command line string. I can't find any alternative windows API for invoking a process like this which takes arguments as an array.

This PR includes modifications to the tests to embed the path to the CMake executable. I needed an executable that took more arguments to validate the handling of spaces in arguments, and this is the only one I could think of that would be available on all platforms.

See https://learn.microsoft.com/en-us/archive/blogs/twistylittlepassagesallalike/everyone-quotes-command-line-arguments-the-wrong-way

The logic upon which this algorithm is based is derived from recommended
practices on an MSDN blog.

Signed-off-by: Scott K Logan <logans@cottsay.net>
@cottsay cottsay added the bug Something isn't working label Apr 1, 2025
@cottsay cottsay self-assigned this Apr 1, 2025
@cottsay
Copy link
Copy Markdown
Member Author

cottsay commented Apr 1, 2025

  • Linux Build Status
  • Linux-aarch64 Build Status
  • Linux-rhel Build Status
  • Windows Build Status

Copy link
Copy Markdown
Collaborator

@fujitatomoya fujitatomoya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm with a couple of comments.

Comment thread src/process.c
Comment on lines +142 to +143
char_array->buffer_length = new_length;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This store is redundant? i believe that line 134 above, rcutils_char_array_expand_as_needed call also uddates the buffer_length with new_length.

Suggested change
char_array->buffer_length = new_length;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It wouldn't change the length, no. The call to _expand_as_needed will update the capacity if necessary, but the length only gets modified if you shrink the array.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah i see. in that case, can we just call rcutils_char_array_resize instead of rcutils_char_array_expand_as_needed to allocate what we need here? i think that updates the buffer_length too. This change ensures that the buffer is resized to exactly new_length, which might be more predictable but could be less efficient if the buffer often already has enough space. (jsut a suggestion, not blocking this PR)

Comment thread src/process.c Outdated
Signed-off-by: Scott K Logan <logans@cottsay.net>
@cottsay
Copy link
Copy Markdown
Member Author

cottsay commented Apr 1, 2025

  • Linux Build Status
  • Linux-aarch64 Build Status
  • Linux-rhel Build Status
  • Windows Build Status

@ahcorde ahcorde merged commit e811760 into rolling Apr 1, 2025
2 checks passed
@ahcorde ahcorde deleted the cottsay/windows-argument-quotes branch April 1, 2025 20:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants