From 0ed48764c132fcc203941a41f28a7d4157d7004f Mon Sep 17 00:00:00 2001 From: Alexander Turenko <alexander.turenko@tarantool.org> Date: Sun, 12 Apr 2020 01:28:46 +0300 Subject: [PATCH] popen: quote multiword command arguments Of course it is still not fair shell-style quoting: at least we should also escape quotes inside arguments. But it gives correct output for most of typical commands and has straightforward implementation. Part of #4031 Acked-by: Cyrill Gorcunov <gorcunov@gmail.com> --- src/lib/core/popen.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/lib/core/popen.c b/src/lib/core/popen.c index 3ba1e2a48c..089c848301 100644 --- a/src/lib/core/popen.c +++ b/src/lib/core/popen.c @@ -154,7 +154,7 @@ handle_new(struct popen_opts *opts) for (i = 0; i < opts->nr_argv; i++) { if (opts->argv[i] == NULL) continue; - size += strlen(opts->argv[i]) + 1; + size += strlen(opts->argv[i]) + 3; } handle = malloc(sizeof(*handle) + size); @@ -168,8 +168,13 @@ handle_new(struct popen_opts *opts) for (i = 0; i < opts->nr_argv-1; i++) { if (opts->argv[i] == NULL) continue; + bool is_multiword = strchr(opts->argv[i], ' ') != NULL; + if (is_multiword) + *pos++ = '\''; strcpy(pos, opts->argv[i]); pos += strlen(opts->argv[i]); + if (is_multiword) + *pos++ = '\''; *pos++ = ' '; } pos[-1] = '\0'; -- GitLab