Index: base/file_path.cc |
diff --git a/base/file_path.cc b/base/file_path.cc |
index 094c699587d36bd83cb509d5b82e7c5383667d04..ec59ff1e5a020dcf1f5dfb326f1a281f71b19872 100644 |
--- a/base/file_path.cc |
+++ b/base/file_path.cc |
@@ -40,6 +40,8 @@ const FilePath::CharType FilePath::kParentDirectory[] = FILE_PATH_LITERAL(".."); |
const FilePath::CharType FilePath::kExtensionSeparator = FILE_PATH_LITERAL('.'); |
+const FilePath::CharType kStringTerminator = FILE_PATH_LITERAL('\0'); |
+ |
Tom Sepez
2012/12/20 21:14:34
Should be in namespace { below.
|
typedef FilePath::StringType StringType; |
namespace { |
@@ -182,6 +184,10 @@ FilePath::FilePath(const FilePath& that) : path_(that.path_) { |
} |
FilePath::FilePath(const StringType& path) : path_(path) { |
+ // Don't allow '\0' characters in path. |
Tom Sepez
2012/12/20 21:14:34
nit: not sure we need this comment. Maybe just add
|
+ StringType::size_type zero_pos = path_.find(kStringTerminator); |
+ if (zero_pos != StringType::npos) |
+ path_.erase(zero_pos); |
Tom Sepez
2012/12/20 21:14:34
nit: maybe write
path_.erase(zero_pos, StringTy
|
} |
FilePath::~FilePath() { |
@@ -454,7 +460,15 @@ bool FilePath::MatchesExtension(const StringType& extension) const { |
} |
FilePath FilePath::Append(const StringType& component) const { |
- DCHECK(!IsPathAbsolute(component)); |
+ StringType appended(component); |
Tom Sepez
2012/12/20 21:14:34
Would be nice to avoid the copy if the NULs not fo
|
+ |
+ // Don't allow '\0' characters to be appended. |
+ StringType::size_type zero_pos = appended.find(kStringTerminator); |
+ if (zero_pos != StringType::npos) |
+ appended.erase(zero_pos); |
+ |
+ DCHECK(!IsPathAbsolute(appended)); |
+ |
if (path_.compare(kCurrentDirectory) == 0) { |
// Append normally doesn't do any normalization, but as a special case, |
// when appending to kCurrentDirectory, just return a new path for the |
@@ -463,7 +477,7 @@ FilePath FilePath::Append(const StringType& component) const { |
// it's likely in practice to wind up with FilePath objects containing |
// only kCurrentDirectory when calling DirName on a single relative path |
// component. |
- return FilePath(component); |
+ return FilePath(appended); |
} |
FilePath new_path(path_); |
@@ -472,7 +486,7 @@ FilePath FilePath::Append(const StringType& component) const { |
// Don't append a separator if the path is empty (indicating the current |
// directory) or if the path component is empty (indicating nothing to |
// append). |
- if (component.length() > 0 && new_path.path_.length() > 0) { |
+ if (appended.length() > 0 && new_path.path_.length() > 0) { |
// Don't append a separator if the path still ends with a trailing |
// separator after stripping (indicating the root directory). |
if (!IsSeparator(new_path.path_[new_path.path_.length() - 1])) { |
@@ -483,7 +497,7 @@ FilePath FilePath::Append(const StringType& component) const { |
} |
} |
- new_path.path_.append(component); |
+ new_path.path_.append(appended); |
return new_path; |
} |