1 |
From e00c55d7a0204dc1d0ae316141323959e1e16162 Mon Sep 17 00:00:00 2001 |
2 |
From: Masatake YAMATO <yamato@redhat.com> |
3 |
Date: Mon, 24 Oct 2016 23:52:23 +0900 |
4 |
Subject: [PATCH] main: quote output file name before passing it to system(3) |
5 |
function |
6 |
|
7 |
Following command line doesn't work: |
8 |
|
9 |
$ ctags -o 'a b' ... |
10 |
|
11 |
because a shell lauched from system(3) deals a whitespace between 'a' |
12 |
and 'b' as a separator. The output file name is passed to system(3) |
13 |
to run external sort command. |
14 |
|
15 |
This commit adds code to put double and single quoets around the output |
16 |
file name before passing it to system(3). |
17 |
|
18 |
The issue is reported by Lorenz Hipp <lhipp@idealbonn.de> in a private mail. |
19 |
|
20 |
Signed-off-by: Masatake YAMATO <yamato@redhat.com> |
21 |
--- |
22 |
Tmain/abnormal-output-file-names.d/input.c | 1 + |
23 |
Tmain/abnormal-output-file-names.d/run.sh | 39 ++++++++++++++ |
24 |
.../stderr-expected.txt | 0 |
25 |
.../stdout-expected.txt | 8 +++ |
26 |
sort.c | 63 ++++++++++++++++++---- |
27 |
5 files changed, 101 insertions(+), 10 deletions(-) |
28 |
create mode 100644 Tmain/abnormal-output-file-names.d/input.c |
29 |
create mode 100644 Tmain/abnormal-output-file-names.d/run.sh |
30 |
create mode 100644 Tmain/abnormal-output-file-names.d/stderr-expected.txt |
31 |
create mode 100644 Tmain/abnormal-output-file-names.d/stdout-expected.txt |
32 |
|
33 |
diff --git a/Tmain/abnormal-output-file-names.d/input.c b/Tmain/abnormal-output-file-names.d/input.c |
34 |
new file mode 100644 |
35 |
index 0000000..6d1a0d4 |
36 |
--- /dev/null |
37 |
+++ b/Tmain/abnormal-output-file-names.d/input.c |
38 |
@@ -0,0 +1 @@ |
39 |
+int x; |
40 |
diff --git a/Tmain/abnormal-output-file-names.d/run.sh b/Tmain/abnormal-output-file-names.d/run.sh |
41 |
new file mode 100644 |
42 |
index 0000000..b15a766 |
43 |
--- /dev/null |
44 |
+++ b/Tmain/abnormal-output-file-names.d/run.sh |
45 |
@@ -0,0 +1,39 @@ |
46 |
+# Copyright: 2016 Masatake YAMATO |
47 |
+# License: GPL-2 |
48 |
+ |
49 |
+CTAGS=$1 |
50 |
+ |
51 |
+rm -f ./"'" |
52 |
+rm -f ./'"' |
53 |
+rm -f ./'$(ls)' |
54 |
+rm -f ./'a b' |
55 |
+ |
56 |
+${CTAGS} --quiet --options=NONE -o ./"'" --extra=-pF input.c |
57 |
+${CTAGS} --quiet --options=NONE -o ./'"' --extra=-pF input.c |
58 |
+${CTAGS} --quiet --options=NONE -o ./'$(ls)' --extra=-pF input.c |
59 |
+${CTAGS} --quiet --options=NONE -o ./'a b' --extra=-pF input.c |
60 |
+ |
61 |
+echo '#' SINGLE QUOTE |
62 |
+if [ -e "'" ]; then |
63 |
+ cat "'" |
64 |
+fi |
65 |
+ |
66 |
+echo '#' DOUBLE QUOTES |
67 |
+if [ -e '"' ]; then |
68 |
+ cat '"' |
69 |
+fi |
70 |
+ |
71 |
+echo '#' PROCESS SUBSTITUTION |
72 |
+if [ -e '$(ls)' ]; then |
73 |
+ cat '$(ls)' |
74 |
+fi |
75 |
+ |
76 |
+echo '#' SPACE |
77 |
+if [ -e 'a b' ]; then |
78 |
+ cat 'a b' |
79 |
+fi |
80 |
+ |
81 |
+rm -f ./"'" |
82 |
+rm -f ./'"' |
83 |
+rm -f ./'$(ls)' |
84 |
+rm -f ./'a b' |
85 |
diff --git a/Tmain/abnormal-output-file-names.d/stderr-expected.txt b/Tmain/abnormal-output-file-names.d/stderr-expected.txt |
86 |
new file mode 100644 |
87 |
index 0000000..e69de29 |
88 |
diff --git a/Tmain/abnormal-output-file-names.d/stdout-expected.txt b/Tmain/abnormal-output-file-names.d/stdout-expected.txt |
89 |
new file mode 100644 |
90 |
index 0000000..5d1129e |
91 |
--- /dev/null |
92 |
+++ b/Tmain/abnormal-output-file-names.d/stdout-expected.txt |
93 |
@@ -0,0 +1,8 @@ |
94 |
+# SINGLE QUOTE |
95 |
+x input.c /^int x;$/;" v typeref:typename:int |
96 |
+# DOUBLE QUOTES |
97 |
+x input.c /^int x;$/;" v typeref:typename:int |
98 |
+# PROCESS SUBSTITUTION |
99 |
+x input.c /^int x;$/;" v typeref:typename:int |
100 |
+# SPACE |
101 |
+x input.c /^int x;$/;" v typeref:typename:int |
102 |
diff --git a/sort.c b/sort.c |
103 |
index c58defc..8d9f5b8 100644 |
104 |
--- a/sort.c |
105 |
+++ b/sort.c |
106 |
@@ -19,6 +19,7 @@ |
107 |
#endif |
108 |
#include <string.h> |
109 |
#include <stdio.h> |
110 |
+#include <stdlib.h> |
111 |
|
112 |
#include "debug.h" |
113 |
#include "entry.h" |
114 |
@@ -53,17 +54,44 @@ extern void catFile (const char *const name) |
115 |
# define PE_CONST const |
116 |
#endif |
117 |
|
118 |
+/* |
119 |
+ Output file name should not be evaluated in system(3) function. |
120 |
+ The name must be used as is. Quotations are required to block the |
121 |
+ evaluation. |
122 |
+ |
123 |
+ Normal single-quotes are used to quote a cstring: |
124 |
+ a => 'a' |
125 |
+ " => '"' |
126 |
+ |
127 |
+ If a single-quote is included in the cstring, use double quotes for quoting it. |
128 |
+ ' => ''"'"'' |
129 |
+*/ |
130 |
+static void appendCstringWithQuotes (vString *dest, const char* cstr) |
131 |
+{ |
132 |
+ const char* o; |
133 |
+ |
134 |
+ vStringPut (dest, '\''); |
135 |
+ for (o = cstr; *o; o++) |
136 |
+ { |
137 |
+ if (*o == '\'') |
138 |
+ vStringCatS (dest, "'\"'\"'"); |
139 |
+ else |
140 |
+ vStringPut (dest, *o); |
141 |
+ } |
142 |
+ vStringPut (dest, '\''); |
143 |
+} |
144 |
+ |
145 |
extern void externalSortTags (const boolean toStdout) |
146 |
{ |
147 |
const char *const sortNormalCommand = "sort -u -o"; |
148 |
const char *const sortFoldedCommand = "sort -u -f -o"; |
149 |
const char *sortCommand = |
150 |
Option.sorted == SO_FOLDSORTED ? sortFoldedCommand : sortNormalCommand; |
151 |
+# ifndef HAVE_SETENV |
152 |
PE_CONST char *const sortOrder1 = "LC_COLLATE=C"; |
153 |
PE_CONST char *const sortOrder2 = "LC_ALL=C"; |
154 |
- const size_t length = 4 + strlen (sortOrder1) + strlen (sortOrder2) + |
155 |
- strlen (sortCommand) + (2 * strlen (tagFileName ())); |
156 |
- char *const cmd = (char *) malloc (length + 1); |
157 |
+# endif |
158 |
+ vString *cmd = vStringNew (); |
159 |
int ret = -1; |
160 |
|
161 |
if (cmd != NULL) |
162 |
@@ -73,20 +101,35 @@ extern void externalSortTags (const boolean toStdout) |
163 |
#ifdef HAVE_SETENV |
164 |
setenv ("LC_COLLATE", "C", 1); |
165 |
setenv ("LC_ALL", "C", 1); |
166 |
- sprintf (cmd, "%s %s %s", sortCommand, tagFileName (), tagFileName ()); |
167 |
+ vStringCatS (cmd, sortCommand); |
168 |
+ vStringPut (cmd, ' '); |
169 |
+ appendCstringWithQuotes (cmd, tagFileName ()); |
170 |
+ vStringPut (cmd, ' '); |
171 |
+ appendCstringWithQuotes (cmd, tagFileName ()); |
172 |
#else |
173 |
# ifdef HAVE_PUTENV |
174 |
putenv (sortOrder1); |
175 |
putenv (sortOrder2); |
176 |
- sprintf (cmd, "%s %s %s", sortCommand, tagFileName (), tagFileName ()); |
177 |
+ vStringCatS (cmd, sortOrder1); |
178 |
+ vStringPut (cmd, ' '); |
179 |
+ appendCstringWithQuotes (cmd, tagFileName ()); |
180 |
+ vStringPut (cmd, ' '); |
181 |
+ appendCstringWithQuotes (cmd, tagFileName ()); |
182 |
# else |
183 |
- sprintf (cmd, "%s %s %s %s %s", sortOrder1, sortOrder2, sortCommand, |
184 |
- tagFileName (), tagFileName ()); |
185 |
+ vStringCatS (cmd, sortOrder1); |
186 |
+ vStringPut (cmd, ' '); |
187 |
+ vStringCatS (cmd, sortOrder2); |
188 |
+ vStringPut (cmd, ' '); |
189 |
+ vStringCatS (cmd, sortCommand); |
190 |
+ vStringPut (cmd, ' '); |
191 |
+ appendCstringWithQuotes (cmd, tagFileName ()); |
192 |
+ vStringPut (cmd, ' '); |
193 |
+ appendCstringWithQuotes (cmd, tagFileName ()); |
194 |
# endif |
195 |
#endif |
196 |
- verbose ("system (\"%s\")\n", cmd); |
197 |
- ret = system (cmd); |
198 |
- free (cmd); |
199 |
+ verbose ("system (\"%s\")\n", vStringValue (cmd)); |
200 |
+ ret = system (vStringValue (cmd)); |
201 |
+ vStringDelete (cmd); |
202 |
|
203 |
} |
204 |
if (ret != 0) |