util: add shell_maybe_quote() call for preparing a string for shell cmdline inclusion
If necessary the passed string is enclosed in "", and all special characters escapes. This also ports over usage in bus-util.c and job.c to use this, instead of a incorrect local implementation that forgets to properly escape.
This commit is contained in:
@@ -1542,6 +1542,24 @@ static void test_sparse_write(void) {
|
||||
test_sparse_write_one(fd, test_e, sizeof(test_e));
|
||||
}
|
||||
|
||||
static void test_shell_maybe_quote_one(const char *s, const char *expected) {
|
||||
_cleanup_free_ char *r;
|
||||
|
||||
assert_se(r = shell_maybe_quote(s));
|
||||
assert_se(streq(r, expected));
|
||||
}
|
||||
|
||||
static void test_shell_maybe_quote(void) {
|
||||
|
||||
test_shell_maybe_quote_one("", "");
|
||||
test_shell_maybe_quote_one("\\", "\"\\\\\"");
|
||||
test_shell_maybe_quote_one("\"", "\"\\\"\"");
|
||||
test_shell_maybe_quote_one("foobar", "foobar");
|
||||
test_shell_maybe_quote_one("foo bar", "\"foo bar\"");
|
||||
test_shell_maybe_quote_one("foo \"bar\" waldo", "\"foo \\\"bar\\\" waldo\"");
|
||||
test_shell_maybe_quote_one("foo$bar", "\"foo\\$bar\"");
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
log_parse_environment();
|
||||
log_open();
|
||||
@@ -1619,6 +1637,7 @@ int main(int argc, char *argv[]) {
|
||||
test_same_fd();
|
||||
test_uid_ptr();
|
||||
test_sparse_write();
|
||||
test_shell_maybe_quote();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user