File tree Expand file tree Collapse file tree 2 files changed +41
-22
lines changed Expand file tree Collapse file tree 2 files changed +41
-22
lines changed Original file line number Diff line number Diff line change @@ -27,16 +27,17 @@ Date: August 2012
27
27
// clang-format on
28
28
#else
29
29
30
- #include < cstring>
31
- #include < cerrno>
32
- #include < cstdio>
33
- #include < cstdlib>
34
-
35
- #include < fcntl.h>
36
- #include < signal.h>
37
- #include < sys/stat.h>
38
- #include < sys/wait.h>
39
- #include < unistd.h>
30
+ # include < sys/stat.h>
31
+ # include < sys/wait.h>
32
+
33
+ # include < cctype>
34
+ # include < cerrno>
35
+ # include < cstdio>
36
+ # include < cstdlib>
37
+ # include < cstring>
38
+ # include < fcntl.h>
39
+ # include < signal.h>
40
+ # include < unistd.h>
40
41
41
42
#endif
42
43
@@ -486,18 +487,18 @@ std::string shell_quote(const std::string &src)
486
487
487
488
// first check if quoting is needed at all
488
489
489
- if (src. find ( ' ' )==std::string::npos &&
490
- src. find ( ' " ' )==std::string::npos &&
491
- src.find ( ' * ' )==std::string::npos &&
492
- src. find ( ' $ ' )==std::string::npos &&
493
- src. find ( ' \\ ' )==std::string::npos &&
494
- src. find ( ' ? ' )==std::string::npos &&
495
- src. find ( ' & ' )==std::string::npos &&
496
- src. find ( ' | ' )==std::string::npos &&
497
- src. find ( ' > ' )==std::string::npos &&
498
- src. find ( ' < ' )==std::string::npos &&
499
- src. find ( ' ^ ' )==std::string::npos &&
500
- src. find ( ' \' ' )==std::string::npos )
490
+ bool quotes_needed = false ;
491
+
492
+ if ( src.empty ())
493
+ quotes_needed = true ;
494
+ else
495
+ {
496
+ for ( auto &ch : src)
497
+ if (! isalnum (ch) && ch != ' _ ' && ch != ' . ' && ch != ' / ' && ch != ' - ' )
498
+ quotes_needed = true ;
499
+ }
500
+
501
+ if (!quotes_needed )
501
502
{
502
503
// seems fine -- return as is
503
504
return src;
Original file line number Diff line number Diff line change @@ -13,6 +13,24 @@ Author: Michael Tautschnig
13
13
14
14
#include < fstream>
15
15
16
+ SCENARIO (" shell_quote() escaping" , " [core][util][run]" )
17
+ {
18
+ #ifdef _WIN32
19
+ REQUIRE (shell_quote (" foo.bar" ) == " foo.bar" );
20
+ REQUIRE (shell_quote (" foo&bar" ) == " \" foo&bar\" " );
21
+ REQUIRE (shell_quote (" foo(bar)" ) == " \" foo(bar)\" " );
22
+ REQUIRE (shell_quote (" foo\" bar" ) == " \" foo\"\" bar\" " );
23
+ #else
24
+ REQUIRE (shell_quote (" foo.bar" ) == " foo.bar" );
25
+ REQUIRE (shell_quote (" foo/bar" ) == " foo/bar" );
26
+ REQUIRE (shell_quote (" --foo" ) == " --foo" );
27
+ REQUIRE (shell_quote (" " ) == " ''" );
28
+ REQUIRE (shell_quote (" foo\n bar" ) == " 'foo\n bar'" );
29
+ REQUIRE (shell_quote (" foo(bar)" ) == " 'foo(bar)'" );
30
+ REQUIRE (shell_quote (" foo'bar" ) == " 'foo'\\ '''bar'" );
31
+ #endif
32
+ }
33
+
16
34
SCENARIO (" run() error reporting" , " [core][util][run]" )
17
35
{
18
36
GIVEN (" A command invoking a non-existent executable" )
You can’t perform that action at this time.
0 commit comments