| 1 |
From 99deb2692099eb0e7cce313ceba17c1eb15d6660 Mon Sep 17 00:00:00 2001 |
| 2 |
From: Masatake YAMATO <[email protected]> |
| 3 |
Date: Mon, 24 Oct 2016 23:52:23 +0900 |
| 4 |
Subject: main: quote output file name before passing it to system(3) function |
| 5 |
|
| 6 |
Following command line doesn't work: |
| 7 |
|
| 8 |
$ ctags -o 'a b' ... |
| 9 |
|
| 10 |
because a shell lauched from system(3) deals a whitespace between 'a' |
| 11 |
and 'b' as a separator. The output file name is passed to system(3) |
| 12 |
to run external sort command. |
| 13 |
|
| 14 |
This commit adds code to put double and single quoets around the output |
| 15 |
file name before passing it to system(3). |
| 16 |
|
| 17 |
The issue is reported by Lorenz Hipp <[email protected]> in a private mail. |
| 18 |
|
| 19 |
Signed-off-by: Masatake YAMATO <[email protected]> |
| 20 |
|
| 21 |
Origin: backport, https://github.com/universal-ctags/ctags/commit/e00c55d7a0204dc1d0ae316141323959e1e16162 |
| 22 |
Bug-Debian: https://bugs.debian.org/1026995 |
| 23 |
Last-Update: 2022-12-26 |
| 24 |
|
| 25 |
Patch-Name: quote-output-file-name.patch |
| 26 |
--- |
| 27 |
sort.c | 53 ++++++++++++++++++++++++++++++++++++++++++----------- |
| 28 |
1 file changed, 42 insertions(+), 11 deletions(-) |
| 29 |
|
| 30 |
diff --git a/sort.c b/sort.c |
| 31 |
index c58defc34..260fbbd21 100644 |
| 32 |
--- a/sort.c |
| 33 |
+++ b/sort.c |
| 34 |
@@ -53,17 +53,44 @@ extern void catFile (const char *const name) |
| 35 |
# define PE_CONST const |
| 36 |
#endif |
| 37 |
|
| 38 |
+/* |
| 39 |
+ Output file name should not be evaluated in system(3) function. |
| 40 |
+ The name must be used as is. Quotations are required to block the |
| 41 |
+ evaluation. |
| 42 |
+ |
| 43 |
+ Normal single-quotes are used to quote a cstring: |
| 44 |
+ a => 'a' |
| 45 |
+ " => '"' |
| 46 |
+ |
| 47 |
+ If a single-quote is included in the cstring, use double quotes for quoting it. |
| 48 |
+ ' => ''"'"'' |
| 49 |
+*/ |
| 50 |
+static void appendCstringWithQuotes (vString *dest, const char* cstr) |
| 51 |
+{ |
| 52 |
+ const char* o; |
| 53 |
+ |
| 54 |
+ vStringPut (dest, '\''); |
| 55 |
+ for (o = cstr; *o; o++) |
| 56 |
+ { |
| 57 |
+ if (*o == '\'') |
| 58 |
+ vStringCatS (dest, "'\"'\"'"); |
| 59 |
+ else |
| 60 |
+ vStringPut (dest, *o); |
| 61 |
+ } |
| 62 |
+ vStringPut (dest, '\''); |
| 63 |
+} |
| 64 |
+ |
| 65 |
extern void externalSortTags (const boolean toStdout) |
| 66 |
{ |
| 67 |
const char *const sortNormalCommand = "sort -u -o"; |
| 68 |
const char *const sortFoldedCommand = "sort -u -f -o"; |
| 69 |
const char *sortCommand = |
| 70 |
Option.sorted == SO_FOLDSORTED ? sortFoldedCommand : sortNormalCommand; |
| 71 |
+# ifndef HAVE_SETENV |
| 72 |
PE_CONST char *const sortOrder1 = "LC_COLLATE=C"; |
| 73 |
PE_CONST char *const sortOrder2 = "LC_ALL=C"; |
| 74 |
- const size_t length = 4 + strlen (sortOrder1) + strlen (sortOrder2) + |
| 75 |
- strlen (sortCommand) + (2 * strlen (tagFileName ())); |
| 76 |
- char *const cmd = (char *) malloc (length + 1); |
| 77 |
+# endif |
| 78 |
+ vString *cmd = vStringNew (); |
| 79 |
int ret = -1; |
| 80 |
|
| 81 |
if (cmd != NULL) |
| 82 |
@@ -73,21 +100,25 @@ extern void externalSortTags (const boolean toStdout) |
| 83 |
#ifdef HAVE_SETENV |
| 84 |
setenv ("LC_COLLATE", "C", 1); |
| 85 |
setenv ("LC_ALL", "C", 1); |
| 86 |
- sprintf (cmd, "%s %s %s", sortCommand, tagFileName (), tagFileName ()); |
| 87 |
#else |
| 88 |
# ifdef HAVE_PUTENV |
| 89 |
putenv (sortOrder1); |
| 90 |
putenv (sortOrder2); |
| 91 |
- sprintf (cmd, "%s %s %s", sortCommand, tagFileName (), tagFileName ()); |
| 92 |
# else |
| 93 |
- sprintf (cmd, "%s %s %s %s %s", sortOrder1, sortOrder2, sortCommand, |
| 94 |
- tagFileName (), tagFileName ()); |
| 95 |
+ vStringCatS (cmd, sortOrder1); |
| 96 |
+ vStringPut (cmd, ' '); |
| 97 |
+ vStringCatS (cmd, sortOrder2); |
| 98 |
+ vStringPut (cmd, ' '); |
| 99 |
# endif |
| 100 |
#endif |
| 101 |
- verbose ("system (\"%s\")\n", cmd); |
| 102 |
- ret = system (cmd); |
| 103 |
- free (cmd); |
| 104 |
- |
| 105 |
+ vStringCatS (cmd, sortCommand); |
| 106 |
+ vStringPut (cmd, ' '); |
| 107 |
+ appendCstringWithQuotes (cmd, tagFileName ()); |
| 108 |
+ vStringPut (cmd, ' '); |
| 109 |
+ appendCstringWithQuotes (cmd, tagFileName ()); |
| 110 |
+ verbose ("system (\"%s\")\n", vStringValue (cmd)); |
| 111 |
+ ret = system (vStringValue (cmd)); |
| 112 |
+ vStringDelete (cmd); |
| 113 |
} |
| 114 |
if (ret != 0) |
| 115 |
error (FATAL | PERROR, "cannot sort tag file"); |