Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(300)

Side by Side Diff: base/file_path.cc

Issue 11642041: Don't allow '\0' characters in FilePath. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/file_path.h" 5 #include "base/file_path.h"
6 6
7 #include <string.h> 7 #include <string.h>
8 #include <algorithm> 8 #include <algorithm>
9 9
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
(...skipping 22 matching lines...) Expand all
33 const FilePath::CharType FilePath::kSeparators[] = FILE_PATH_LITERAL("\\/"); 33 const FilePath::CharType FilePath::kSeparators[] = FILE_PATH_LITERAL("\\/");
34 #else // FILE_PATH_USES_WIN_SEPARATORS 34 #else // FILE_PATH_USES_WIN_SEPARATORS
35 const FilePath::CharType FilePath::kSeparators[] = FILE_PATH_LITERAL("/"); 35 const FilePath::CharType FilePath::kSeparators[] = FILE_PATH_LITERAL("/");
36 #endif // FILE_PATH_USES_WIN_SEPARATORS 36 #endif // FILE_PATH_USES_WIN_SEPARATORS
37 37
38 const FilePath::CharType FilePath::kCurrentDirectory[] = FILE_PATH_LITERAL("."); 38 const FilePath::CharType FilePath::kCurrentDirectory[] = FILE_PATH_LITERAL(".");
39 const FilePath::CharType FilePath::kParentDirectory[] = FILE_PATH_LITERAL(".."); 39 const FilePath::CharType FilePath::kParentDirectory[] = FILE_PATH_LITERAL("..");
40 40
41 const FilePath::CharType FilePath::kExtensionSeparator = FILE_PATH_LITERAL('.'); 41 const FilePath::CharType FilePath::kExtensionSeparator = FILE_PATH_LITERAL('.');
42 42
43 const FilePath::CharType kStringTerminator = FILE_PATH_LITERAL('\0');
44
Tom Sepez 2012/12/20 21:14:34 Should be in namespace { below.
43 typedef FilePath::StringType StringType; 45 typedef FilePath::StringType StringType;
44 46
45 namespace { 47 namespace {
46 48
47 const char* kCommonDoubleExtensionSuffixes[] = { "gz", "z", "bz2" }; 49 const char* kCommonDoubleExtensionSuffixes[] = { "gz", "z", "bz2" };
48 const char* kCommonDoubleExtensions[] = { "user.js" }; 50 const char* kCommonDoubleExtensions[] = { "user.js" };
49 51
50 // If this FilePath contains a drive letter specification, returns the 52 // If this FilePath contains a drive letter specification, returns the
51 // position of the last character of the drive letter specification, 53 // position of the last character of the drive letter specification,
52 // otherwise returns npos. This can only be true on Windows, when a pathname 54 // otherwise returns npos. This can only be true on Windows, when a pathname
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 177
176 } // namespace 178 } // namespace
177 179
178 FilePath::FilePath() { 180 FilePath::FilePath() {
179 } 181 }
180 182
181 FilePath::FilePath(const FilePath& that) : path_(that.path_) { 183 FilePath::FilePath(const FilePath& that) : path_(that.path_) {
182 } 184 }
183 185
184 FilePath::FilePath(const StringType& path) : path_(path) { 186 FilePath::FilePath(const StringType& path) : path_(path) {
187 // 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
188 StringType::size_type zero_pos = path_.find(kStringTerminator);
189 if (zero_pos != StringType::npos)
190 path_.erase(zero_pos);
Tom Sepez 2012/12/20 21:14:34 nit: maybe write path_.erase(zero_pos, StringTy
185 } 191 }
186 192
187 FilePath::~FilePath() { 193 FilePath::~FilePath() {
188 } 194 }
189 195
190 FilePath& FilePath::operator=(const FilePath& that) { 196 FilePath& FilePath::operator=(const FilePath& that) {
191 path_ = that.path_; 197 path_ = that.path_;
192 return *this; 198 return *this;
193 } 199 }
194 200
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after
447 453
448 StringType current_extension = Extension(); 454 StringType current_extension = Extension();
449 455
450 if (current_extension.length() != extension.length()) 456 if (current_extension.length() != extension.length())
451 return false; 457 return false;
452 458
453 return FilePath::CompareEqualIgnoreCase(extension, current_extension); 459 return FilePath::CompareEqualIgnoreCase(extension, current_extension);
454 } 460 }
455 461
456 FilePath FilePath::Append(const StringType& component) const { 462 FilePath FilePath::Append(const StringType& component) const {
457 DCHECK(!IsPathAbsolute(component)); 463 StringType appended(component);
Tom Sepez 2012/12/20 21:14:34 Would be nice to avoid the copy if the NULs not fo
464
465 // Don't allow '\0' characters to be appended.
466 StringType::size_type zero_pos = appended.find(kStringTerminator);
467 if (zero_pos != StringType::npos)
468 appended.erase(zero_pos);
469
470 DCHECK(!IsPathAbsolute(appended));
471
458 if (path_.compare(kCurrentDirectory) == 0) { 472 if (path_.compare(kCurrentDirectory) == 0) {
459 // Append normally doesn't do any normalization, but as a special case, 473 // Append normally doesn't do any normalization, but as a special case,
460 // when appending to kCurrentDirectory, just return a new path for the 474 // when appending to kCurrentDirectory, just return a new path for the
461 // component argument. Appending component to kCurrentDirectory would 475 // component argument. Appending component to kCurrentDirectory would
462 // serve no purpose other than needlessly lengthening the path, and 476 // serve no purpose other than needlessly lengthening the path, and
463 // it's likely in practice to wind up with FilePath objects containing 477 // it's likely in practice to wind up with FilePath objects containing
464 // only kCurrentDirectory when calling DirName on a single relative path 478 // only kCurrentDirectory when calling DirName on a single relative path
465 // component. 479 // component.
466 return FilePath(component); 480 return FilePath(appended);
467 } 481 }
468 482
469 FilePath new_path(path_); 483 FilePath new_path(path_);
470 new_path.StripTrailingSeparatorsInternal(); 484 new_path.StripTrailingSeparatorsInternal();
471 485
472 // Don't append a separator if the path is empty (indicating the current 486 // Don't append a separator if the path is empty (indicating the current
473 // directory) or if the path component is empty (indicating nothing to 487 // directory) or if the path component is empty (indicating nothing to
474 // append). 488 // append).
475 if (component.length() > 0 && new_path.path_.length() > 0) { 489 if (appended.length() > 0 && new_path.path_.length() > 0) {
476 // Don't append a separator if the path still ends with a trailing 490 // Don't append a separator if the path still ends with a trailing
477 // separator after stripping (indicating the root directory). 491 // separator after stripping (indicating the root directory).
478 if (!IsSeparator(new_path.path_[new_path.path_.length() - 1])) { 492 if (!IsSeparator(new_path.path_[new_path.path_.length() - 1])) {
479 // Don't append a separator if the path is just a drive letter. 493 // Don't append a separator if the path is just a drive letter.
480 if (FindDriveLetter(new_path.path_) + 1 != new_path.path_.length()) { 494 if (FindDriveLetter(new_path.path_) + 1 != new_path.path_.length()) {
481 new_path.path_.append(1, kSeparators[0]); 495 new_path.path_.append(1, kSeparators[0]);
482 } 496 }
483 } 497 }
484 } 498 }
485 499
486 new_path.path_.append(component); 500 new_path.path_.append(appended);
487 return new_path; 501 return new_path;
488 } 502 }
489 503
490 FilePath FilePath::Append(const FilePath& component) const { 504 FilePath FilePath::Append(const FilePath& component) const {
491 return Append(component.value()); 505 return Append(component.value());
492 } 506 }
493 507
494 FilePath FilePath::AppendASCII(const base::StringPiece& component) const { 508 FilePath FilePath::AppendASCII(const base::StringPiece& component) const {
495 DCHECK(IsStringASCII(component)); 509 DCHECK(IsStringASCII(component));
496 #if defined(OS_WIN) 510 #if defined(OS_WIN)
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
593 #if defined(OS_WIN) 607 #if defined(OS_WIN)
594 pickle->WriteString16(path_); 608 pickle->WriteString16(path_);
595 #else 609 #else
596 pickle->WriteString(path_); 610 pickle->WriteString(path_);
597 #endif 611 #endif
598 } 612 }
599 613
600 bool FilePath::ReadFromPickle(PickleIterator* iter) { 614 bool FilePath::ReadFromPickle(PickleIterator* iter) {
601 #if defined(OS_WIN) 615 #if defined(OS_WIN)
602 if (!iter->ReadString16(&path_)) 616 if (!iter->ReadString16(&path_))
603 return false; 617 return false;
Tom Sepez 2012/12/20 21:14:34 Need the check here, otherwise IPCs bypass your co
Tom Sepez 2012/12/20 21:19:39 Or Not. Looks like IPC reads the underlying strin
604 #else 618 #else
605 if (!iter->ReadString(&path_)) 619 if (!iter->ReadString(&path_))
606 return false; 620 return false;
607 #endif 621 #endif
608 622
609 return true; 623 return true;
610 } 624 }
611 625
612 #if defined(OS_WIN) 626 #if defined(OS_WIN)
613 // Windows specific implementation of file string comparisons 627 // Windows specific implementation of file string comparisons
(...skipping 615 matching lines...) Expand 10 before | Expand all | Expand 10 after
1229 } 1243 }
1230 return FilePath(copy); 1244 return FilePath(copy);
1231 #else 1245 #else
1232 return *this; 1246 return *this;
1233 #endif 1247 #endif
1234 } 1248 }
1235 1249
1236 void PrintTo(const FilePath& path, std::ostream* out) { 1250 void PrintTo(const FilePath& path, std::ostream* out) {
1237 *out << path.value(); 1251 *out << path.value();
1238 } 1252 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698