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

Unified Diff: base/files/file_path.cc

Issue 1836923002: Handle invalid CFStrings in GetHFSDecomposedForm() (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix test Created 4 years, 9 months 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | base/files/file_path_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/files/file_path.cc
diff --git a/base/files/file_path.cc b/base/files/file_path.cc
index de431232872f2c96fdf947b5c376ab4a84e78235..2c199e131a56ee1976dded1dd25af158ac1c6252 100644
--- a/base/files/file_path.cc
+++ b/base/files/file_path.cc
@@ -1192,6 +1192,7 @@ int FilePath::HFSFastUnicodeCompare(StringPieceType string1,
}
StringType FilePath::GetHFSDecomposedForm(StringPieceType string) {
+ StringType result;
ScopedCFTypeRef<CFStringRef> cfstring(
CFStringCreateWithBytesNoCopy(
NULL,
@@ -1200,26 +1201,27 @@ StringType FilePath::GetHFSDecomposedForm(StringPieceType string) {
kCFStringEncodingUTF8,
false,
kCFAllocatorNull));
- // Query the maximum length needed to store the result. In most cases this
- // will overestimate the required space. The return value also already
- // includes the space needed for a terminating 0.
- CFIndex length = CFStringGetMaximumSizeOfFileSystemRepresentation(cfstring);
- DCHECK_GT(length, 0); // should be at least 1 for the 0-terminator.
- // Reserve enough space for CFStringGetFileSystemRepresentation to write into.
- // Also set the length to the maximum so that we can shrink it later.
- // (Increasing rather than decreasing it would clobber the string contents!)
- StringType result;
- result.reserve(length);
- result.resize(length - 1);
- Boolean success = CFStringGetFileSystemRepresentation(cfstring,
- &result[0],
- length);
- if (success) {
- // Reduce result.length() to actual string length.
- result.resize(strlen(result.c_str()));
- } else {
- // An error occurred -> clear result.
- result.clear();
+ if (cfstring) {
+ // Query the maximum length needed to store the result. In most cases this
+ // will overestimate the required space. The return value also already
+ // includes the space needed for a terminating 0.
+ CFIndex length = CFStringGetMaximumSizeOfFileSystemRepresentation(cfstring);
+ DCHECK_GT(length, 0); // should be at least 1 for the 0-terminator.
+ // Reserve enough space for CFStringGetFileSystemRepresentation to write
+ // into. Also set the length to the maximum so that we can shrink it later.
+ // (Increasing rather than decreasing it would clobber the string contents!)
+ result.reserve(length);
+ result.resize(length - 1);
+ Boolean success = CFStringGetFileSystemRepresentation(cfstring,
+ &result[0],
+ length);
+ if (success) {
+ // Reduce result.length() to actual string length.
+ result.resize(strlen(result.c_str()));
+ } else {
+ // An error occurred -> clear result.
+ result.clear();
+ }
}
return result;
}
« no previous file with comments | « no previous file | base/files/file_path_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698