Skip to content

ROX-32841: Quote args#295

Draft
ovalenti wants to merge 3 commits intomainfrom
ovalenti/ROX-32841-quote-args
Draft

ROX-32841: Quote args#295
ovalenti wants to merge 3 commits intomainfrom
ovalenti/ROX-32841-quote-args

Conversation

@ovalenti
Copy link
Contributor

Description

Introducing proper quoting of arguments when serializing the command-line.

Checklist

  • Investigated and inspected CI test results
  • Updated documentation accordingly

Automated testing

  • Added unit tests
  • Added integration tests
  • Added regression tests

@ovalenti ovalenti self-assigned this Feb 16, 2026
Copy link
Contributor

@Molter73 Molter73 left a comment

Choose a reason for hiding this comment

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

Couple small comments, overall looks good! Thanks for tackling this.

Comment on lines +29 to +41
def rust_style_quote(s):
if not s:
return "''"
if re.search(r'[^a-zA-Z0-9_./-]', s):
# Try to match the behavior of shlex.try_join()
if '\'' in s and not '"' in s:
return f'"{s}"'
escaped = s.replace("'", "\\'")
return f"'{escaped}'"
return s

def rust_style_join(args):
return ' '.join(rust_style_quote(arg) for arg in args)
Copy link
Contributor

Choose a reason for hiding this comment

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

Have you tried using Python's shlex and see if it behaves the same way as the Rust implementation? It would be a lot easier for tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is what I used initially, and their quoting strategy is vastly different, unfortunately.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, "vastly" was maybe a bit too strong : the "safe" characters list differ, in particular rust considers '=' and ':' unsafe.

This behavior is not tunable AFAICT, so I resorted to mimic things on the testing side.

Copy link
Contributor

Choose a reason for hiding this comment

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

Could you put a comment detailing this so the next person doesn't try to use Python's shlex?

Comment on lines +197 to +198
let args =
shlex::try_join(args.iter().map(|s| s.as_str())).unwrap_or_else(|_| String::new());
Copy link
Contributor

Choose a reason for hiding this comment

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

Since the parsing of arguments received from the kernel is done by searching for null characters, I don't think it is possible for shlex::try_join to fail here, because we've already removed them all. If that's the case, this might be better:

Suggested change
let args =
shlex::try_join(args.iter().map(|s| s.as_str())).unwrap_or_else(|_| String::new());
let Ok(args) = shlex::try_join(args.iter().map(|s| s.as_str())) else {
unreachable!();
};

Probably also want to update the comment to reflect this.

@Molter73
Copy link
Contributor

BTW, I'm working on improving the output of test errors because it is horrible at the moment: #296

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.

2 participants