protect invalid characters from shell

This commit is contained in:
David Rose 2002-08-14 14:00:56 +00:00
parent 55a83870ee
commit 6eae3155e2
2 changed files with 46 additions and 3 deletions

View File

@ -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

View File

@ -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();