Fossil SCM

Begin splitting up fossil_assert_safe_command_string() into separate windows and unix implementions. Add the test-fossil-system command for testing.

drh 2020-06-09 16:47 trunk
Commit 9c38a004adefea005ba1475952c3768fe18ffec531dfe9773100667bf483c743
1 file changed +29 -2
+29 -2
--- src/util.c
+++ src/util.c
@@ -158,11 +158,11 @@
158158
return zStart;
159159
}
160160
161161
/*
162162
** Check the input string to ensure that it is safe to pass into system().
163
-** A string is unsafe for system() if it contains any of the following:
163
+** A string is unsafe for system() on unix if it contains any of the following:
164164
**
165165
** * Any occurrance of '$' or '`' except after \
166166
** * Any of the following characters, unquoted: ;|& or \n except
167167
** these characters are allowed as the very last character in the
168168
** string.
@@ -174,13 +174,15 @@
174174
** safety net in case of bugs elsewhere in the system.
175175
**
176176
** If an unsafe string is seen, the process aborts.
177177
*/
178178
void fossil_assert_safe_command_string(const char *z){
179
+ int unsafe = 0;
180
+#ifndef _WIN32
181
+ /* Unix */
179182
int inQuote = 0;
180183
int i, c;
181
- int unsafe = 0;
182184
for(i=0; (c = z[i])!=0; i++){
183185
switch( c ){
184186
case '$':
185187
case '`': {
186188
unsafe = i+1;
@@ -210,10 +212,14 @@
210212
}
211213
break;
212214
}
213215
}
214216
}
217
+#else
218
+ /* Windows */
219
+
220
+#endif
215221
if( unsafe ){
216222
fossil_fatal("Unsafe command string: %s\n%*shere ----^",
217223
z, unsafe+13, "");
218224
}
219225
}
@@ -254,10 +260,31 @@
254260
rc = system(zOrigCmd);
255261
fossil_limit_memory(1);
256262
#endif
257263
return rc;
258264
}
265
+
266
+/*
267
+** COMMAND: test-fossil-system
268
+**
269
+** Read lines of input and send them to fossil_system() for evaluation.
270
+*/
271
+void test_fossil_system_cmd(void){
272
+ char zLine[10000];
273
+ while(1){
274
+ size_t n;
275
+ printf("system-test> ");
276
+ fflush(stdout);
277
+ if( !fgets(zLine, sizeof(zLine), stdin) ) break;
278
+ n = strlen(zLine);
279
+ while( n>0 && fossil_isspace(zLine[n-1]) ) n--;
280
+ zLine[n] = 0;
281
+ printf("cmd: [%s]\n", zLine);
282
+ fflush(stdout);
283
+ fossil_system(zLine);
284
+ }
285
+}
259286
260287
/*
261288
** Like strcmp() except that it accepts NULL pointers. NULL sorts before
262289
** all non-NULL string pointers. Also, this strcmp() is a binary comparison
263290
** that does not consider locale.
264291
--- src/util.c
+++ src/util.c
@@ -158,11 +158,11 @@
158 return zStart;
159 }
160
161 /*
162 ** Check the input string to ensure that it is safe to pass into system().
163 ** A string is unsafe for system() if it contains any of the following:
164 **
165 ** * Any occurrance of '$' or '`' except after \
166 ** * Any of the following characters, unquoted: ;|& or \n except
167 ** these characters are allowed as the very last character in the
168 ** string.
@@ -174,13 +174,15 @@
174 ** safety net in case of bugs elsewhere in the system.
175 **
176 ** If an unsafe string is seen, the process aborts.
177 */
178 void fossil_assert_safe_command_string(const char *z){
 
 
 
179 int inQuote = 0;
180 int i, c;
181 int unsafe = 0;
182 for(i=0; (c = z[i])!=0; i++){
183 switch( c ){
184 case '$':
185 case '`': {
186 unsafe = i+1;
@@ -210,10 +212,14 @@
210 }
211 break;
212 }
213 }
214 }
 
 
 
 
215 if( unsafe ){
216 fossil_fatal("Unsafe command string: %s\n%*shere ----^",
217 z, unsafe+13, "");
218 }
219 }
@@ -254,10 +260,31 @@
254 rc = system(zOrigCmd);
255 fossil_limit_memory(1);
256 #endif
257 return rc;
258 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
259
260 /*
261 ** Like strcmp() except that it accepts NULL pointers. NULL sorts before
262 ** all non-NULL string pointers. Also, this strcmp() is a binary comparison
263 ** that does not consider locale.
264
--- src/util.c
+++ src/util.c
@@ -158,11 +158,11 @@
158 return zStart;
159 }
160
161 /*
162 ** Check the input string to ensure that it is safe to pass into system().
163 ** A string is unsafe for system() on unix if it contains any of the following:
164 **
165 ** * Any occurrance of '$' or '`' except after \
166 ** * Any of the following characters, unquoted: ;|& or \n except
167 ** these characters are allowed as the very last character in the
168 ** string.
@@ -174,13 +174,15 @@
174 ** safety net in case of bugs elsewhere in the system.
175 **
176 ** If an unsafe string is seen, the process aborts.
177 */
178 void fossil_assert_safe_command_string(const char *z){
179 int unsafe = 0;
180 #ifndef _WIN32
181 /* Unix */
182 int inQuote = 0;
183 int i, c;
 
184 for(i=0; (c = z[i])!=0; i++){
185 switch( c ){
186 case '$':
187 case '`': {
188 unsafe = i+1;
@@ -210,10 +212,14 @@
212 }
213 break;
214 }
215 }
216 }
217 #else
218 /* Windows */
219
220 #endif
221 if( unsafe ){
222 fossil_fatal("Unsafe command string: %s\n%*shere ----^",
223 z, unsafe+13, "");
224 }
225 }
@@ -254,10 +260,31 @@
260 rc = system(zOrigCmd);
261 fossil_limit_memory(1);
262 #endif
263 return rc;
264 }
265
266 /*
267 ** COMMAND: test-fossil-system
268 **
269 ** Read lines of input and send them to fossil_system() for evaluation.
270 */
271 void test_fossil_system_cmd(void){
272 char zLine[10000];
273 while(1){
274 size_t n;
275 printf("system-test> ");
276 fflush(stdout);
277 if( !fgets(zLine, sizeof(zLine), stdin) ) break;
278 n = strlen(zLine);
279 while( n>0 && fossil_isspace(zLine[n-1]) ) n--;
280 zLine[n] = 0;
281 printf("cmd: [%s]\n", zLine);
282 fflush(stdout);
283 fossil_system(zLine);
284 }
285 }
286
287 /*
288 ** Like strcmp() except that it accepts NULL pointers. NULL sorts before
289 ** all non-NULL string pointers. Also, this strcmp() is a binary comparison
290 ** that does not consider locale.
291

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button