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

Side by Side 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, 8 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 unified diff | Download patch
« no previous file with comments | « no previous file | base/files/file_path_unittest.cc » ('j') | 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/files/file_path.h" 5 #include "base/files/file_path.h"
6 6
7 #include <string.h> 7 #include <string.h>
8 #include <algorithm> 8 #include <algorithm>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 1174 matching lines...) Expand 10 before | Expand all | Expand 10 after
1185 return (codepoint1 < codepoint2) ? -1 : 1; 1185 return (codepoint1 < codepoint2) ? -1 : 1;
1186 if (codepoint1 == 0) { 1186 if (codepoint1 == 0) {
1187 DCHECK_EQ(index1, length1); 1187 DCHECK_EQ(index1, length1);
1188 DCHECK_EQ(index2, length2); 1188 DCHECK_EQ(index2, length2);
1189 return 0; 1189 return 0;
1190 } 1190 }
1191 } 1191 }
1192 } 1192 }
1193 1193
1194 StringType FilePath::GetHFSDecomposedForm(StringPieceType string) { 1194 StringType FilePath::GetHFSDecomposedForm(StringPieceType string) {
1195 StringType result;
1195 ScopedCFTypeRef<CFStringRef> cfstring( 1196 ScopedCFTypeRef<CFStringRef> cfstring(
1196 CFStringCreateWithBytesNoCopy( 1197 CFStringCreateWithBytesNoCopy(
1197 NULL, 1198 NULL,
1198 reinterpret_cast<const UInt8*>(string.data()), 1199 reinterpret_cast<const UInt8*>(string.data()),
1199 string.length(), 1200 string.length(),
1200 kCFStringEncodingUTF8, 1201 kCFStringEncodingUTF8,
1201 false, 1202 false,
1202 kCFAllocatorNull)); 1203 kCFAllocatorNull));
1203 // Query the maximum length needed to store the result. In most cases this 1204 if (cfstring) {
1204 // will overestimate the required space. The return value also already 1205 // Query the maximum length needed to store the result. In most cases this
1205 // includes the space needed for a terminating 0. 1206 // will overestimate the required space. The return value also already
1206 CFIndex length = CFStringGetMaximumSizeOfFileSystemRepresentation(cfstring); 1207 // includes the space needed for a terminating 0.
1207 DCHECK_GT(length, 0); // should be at least 1 for the 0-terminator. 1208 CFIndex length = CFStringGetMaximumSizeOfFileSystemRepresentation(cfstring);
1208 // Reserve enough space for CFStringGetFileSystemRepresentation to write into. 1209 DCHECK_GT(length, 0); // should be at least 1 for the 0-terminator.
1209 // Also set the length to the maximum so that we can shrink it later. 1210 // Reserve enough space for CFStringGetFileSystemRepresentation to write
1210 // (Increasing rather than decreasing it would clobber the string contents!) 1211 // into. Also set the length to the maximum so that we can shrink it later.
1211 StringType result; 1212 // (Increasing rather than decreasing it would clobber the string contents!)
1212 result.reserve(length); 1213 result.reserve(length);
1213 result.resize(length - 1); 1214 result.resize(length - 1);
1214 Boolean success = CFStringGetFileSystemRepresentation(cfstring, 1215 Boolean success = CFStringGetFileSystemRepresentation(cfstring,
1215 &result[0], 1216 &result[0],
1216 length); 1217 length);
1217 if (success) { 1218 if (success) {
1218 // Reduce result.length() to actual string length. 1219 // Reduce result.length() to actual string length.
1219 result.resize(strlen(result.c_str())); 1220 result.resize(strlen(result.c_str()));
1220 } else { 1221 } else {
1221 // An error occurred -> clear result. 1222 // An error occurred -> clear result.
1222 result.clear(); 1223 result.clear();
1224 }
1223 } 1225 }
1224 return result; 1226 return result;
1225 } 1227 }
1226 1228
1227 int FilePath::CompareIgnoreCase(StringPieceType string1, 1229 int FilePath::CompareIgnoreCase(StringPieceType string1,
1228 StringPieceType string2) { 1230 StringPieceType string2) {
1229 // Quick checks for empty strings - these speed things up a bit and make the 1231 // Quick checks for empty strings - these speed things up a bit and make the
1230 // following code cleaner. 1232 // following code cleaner.
1231 if (string1.empty()) 1233 if (string1.empty())
1232 return string2.empty() ? 0 : -1; 1234 return string2.empty() ? 0 : -1;
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
1321 #endif 1323 #endif
1322 } 1324 }
1323 1325
1324 #if defined(OS_ANDROID) 1326 #if defined(OS_ANDROID)
1325 bool FilePath::IsContentUri() const { 1327 bool FilePath::IsContentUri() const {
1326 return StartsWith(path_, "content://", base::CompareCase::INSENSITIVE_ASCII); 1328 return StartsWith(path_, "content://", base::CompareCase::INSENSITIVE_ASCII);
1327 } 1329 }
1328 #endif 1330 #endif
1329 1331
1330 } // namespace base 1332 } // namespace base
OLDNEW
« 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