Procházet zdrojové kódy

protect invalid characters from shell

David Rose před 23 roky
rodič
revize
6eae3155e2
2 změnil soubory, kde provedl 46 přidání a 3 odebrání
  1. 45 3
      pandatool/src/cvscopy/cvsCopy.cxx
  2. 1 0
      pandatool/src/cvscopy/cvsCopy.h

+ 45 - 3
pandatool/src/cvscopy/cvsCopy.cxx

@@ -342,14 +342,13 @@ cvs_add(const Filename &filename) {
     return true;
     return true;
   }
   }
 
 
-  Filename canon = filename;
-
   if (!CVSSourceTree::temp_chdir(filename.get_dirname())) {
   if (!CVSSourceTree::temp_chdir(filename.get_dirname())) {
     nout << "Invalid directory: " << filename.get_dirname() << "\n";
     nout << "Invalid directory: " << filename.get_dirname() << "\n";
     return false;
     return false;
   }
   }
 
 
-  string command = _cvs_binary + " add -kb " + filename.get_basename();
+  string command = _cvs_binary + " add -kb " + 
+    protect_from_shell(filename.get_basename());
   nout << command << "\n";
   nout << command << "\n";
   int result = system(command.c_str());
   int result = system(command.c_str());
 
 
@@ -362,6 +361,49 @@ cvs_add(const Filename &filename) {
   return true;
   return true;
 }
 }
 
 
+////////////////////////////////////////////////////////////////////
+//     Function: CVSCopy::protect_from_shell
+//       Access: Protected, Static
+//  Description: Inserts escape characters into the indicated source
+//               string to protect it from the shell, so that it may
+//               be given on the command line.  Returns the modified
+//               string.
+////////////////////////////////////////////////////////////////////
+string CVSCopy::
+protect_from_shell(const string &source) {
+  string result;
+
+  for (string::const_iterator pi = source.begin(); pi != source.end(); ++pi) {
+    switch (*pi) {
+    case '\\':
+    case ' ':
+    case '\'':
+    case '"':
+    case '(':
+    case ')':
+    case '<':
+    case '>':
+    case '|':
+    case '&':
+    case '!':
+    case '$':
+    case '~':
+    case '*':
+    case '?':
+    case '[':
+    case ']':
+    case ';':
+      result += '\\';
+      // fall through
+
+    default:
+      result += *pi;
+    }
+  }
+
+  return result;
+}
+
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: CVSCopy::scan_hierarchy
 //     Function: CVSCopy::scan_hierarchy
 //       Access: Private
 //       Access: Private

+ 1 - 0
pandatool/src/cvscopy/cvsCopy.h

@@ -56,6 +56,7 @@ protected:
   bool copy_binary_file(Filename source, Filename dest);
   bool copy_binary_file(Filename source, Filename dest);
 
 
   bool cvs_add(const Filename &filename);
   bool cvs_add(const Filename &filename);
+  static string protect_from_shell(const string &source);
 
 
 private:
 private:
   bool scan_hierarchy();
   bool scan_hierarchy();