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

Side by Side Diff: base/command_line.cc

Issue 1046363002: Enforce lowercase switches when calling CommandLine::HasSwitch(const char*). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove EXPECT_DEBUG_DEATH Created 5 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/command_line_unittest.cc » ('j') | base/command_line_unittest.cc » ('J')
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/command_line.h" 5 #include "base/command_line.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <ostream> 8 #include <ostream>
9 9
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 switch_value); 86 switch_value);
87 #elif defined(OS_POSIX) 87 #elif defined(OS_POSIX)
88 command_line->AppendSwitchNative(switch_string, switch_value); 88 command_line->AppendSwitchNative(switch_string, switch_value);
89 #endif 89 #endif
90 } else { 90 } else {
91 command_line->AppendArgNative(arg); 91 command_line->AppendArgNative(arg);
92 } 92 }
93 } 93 }
94 } 94 }
95 95
96 bool ContainsUpperASCII(const char* string) {
97 for (; *string; ++string) {
98 if (*string >= 'A' && *string <= 'Z')
99 return true;
100 }
101 return false;
102 }
103
96 // Lowercase switches for backwards compatiblity *on Windows*. 104 // Lowercase switches for backwards compatiblity *on Windows*.
97 #if defined(OS_WIN) 105 #if defined(OS_WIN)
98 std::string LowerASCIIOnWindows(const std::string& string) { 106 std::string LowerASCIIOnWindows(const std::string& string) {
99 return StringToLowerASCII(string); 107 return StringToLowerASCII(string);
100 } 108 }
101 #elif defined(OS_POSIX) 109 #elif defined(OS_POSIX)
102 const std::string& LowerASCIIOnWindows(const std::string& string) { 110 const std::string& LowerASCIIOnWindows(const std::string& string) {
103 return string; 111 return string;
104 } 112 }
105 #endif 113 #endif
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 } 263 }
256 264
257 FilePath CommandLine::GetProgram() const { 265 FilePath CommandLine::GetProgram() const {
258 return FilePath(argv_[0]); 266 return FilePath(argv_[0]);
259 } 267 }
260 268
261 void CommandLine::SetProgram(const FilePath& program) { 269 void CommandLine::SetProgram(const FilePath& program) {
262 TrimWhitespace(program.value(), TRIM_ALL, &argv_[0]); 270 TrimWhitespace(program.value(), TRIM_ALL, &argv_[0]);
263 } 271 }
264 272
265 bool CommandLine::HasSwitch(const std::string& switch_string) const { 273 bool CommandLine::HasSwitch(const std::string& switch_string) const {
tapted 2015/04/02 01:46:14 How many times is the std::string version called n
jackhou1 2015/04/02 04:51:51 Agreed. 1% of calls.
266 return switches_.find(LowerASCIIOnWindows(switch_string)) != switches_.end(); 274 #if defined(OS_WIN)
275 if (ContainsUpperASCII(switch_string.c_str()))
276 return switches_.find(LowerASCIIOnWindows(switch_string)) !=
277 switches_.end();
278 #endif
279
280 return switches_.find(switch_string) != switches_.end();
267 } 281 }
268 282
269 bool CommandLine::HasSwitch(const char string_constant[]) const { 283 bool CommandLine::HasSwitch(const char string_constant[]) const {
270 return HasSwitch(std::string(string_constant)); 284 DCHECK(!ContainsUpperASCII(string_constant));
tapted 2015/04/02 01:46:14 I think we can avoid the extra function with somet
jackhou1 2015/04/02 04:51:51 Done.
285 return switches_.find(std::string(string_constant)) != switches_.end();
tapted 2015/04/02 01:46:15 is the std::string(..) still needed?
jackhou1 2015/04/02 04:51:51 Nope, but the one in the DEBUG_EQ is.
271 } 286 }
272 287
273 std::string CommandLine::GetSwitchValueASCII( 288 std::string CommandLine::GetSwitchValueASCII(
274 const std::string& switch_string) const { 289 const std::string& switch_string) const {
275 StringType value = GetSwitchValueNative(switch_string); 290 StringType value = GetSwitchValueNative(switch_string);
276 if (!IsStringASCII(value)) { 291 if (!IsStringASCII(value)) {
277 DLOG(WARNING) << "Value of switch (" << switch_string << ") must be ASCII."; 292 DLOG(WARNING) << "Value of switch (" << switch_string << ") must be ASCII.";
278 return std::string(); 293 return std::string();
279 } 294 }
280 #if defined(OS_WIN) 295 #if defined(OS_WIN)
281 return UTF16ToASCII(value); 296 return UTF16ToASCII(value);
282 #else 297 #else
283 return value; 298 return value;
284 #endif 299 #endif
285 } 300 }
286 301
287 FilePath CommandLine::GetSwitchValuePath( 302 FilePath CommandLine::GetSwitchValuePath(
288 const std::string& switch_string) const { 303 const std::string& switch_string) const {
289 return FilePath(GetSwitchValueNative(switch_string)); 304 return FilePath(GetSwitchValueNative(switch_string));
290 } 305 }
291 306
292 CommandLine::StringType CommandLine::GetSwitchValueNative( 307 CommandLine::StringType CommandLine::GetSwitchValueNative(
293 const std::string& switch_string) const { 308 const std::string& switch_string) const {
294 SwitchMap::const_iterator result = 309 #if defined(OS_WIN)
tapted 2015/04/02 01:46:14 I think we should defer this until there's a char[
jackhou1 2015/04/02 04:51:51 This method is about 9% of lookups during startup,
295 switches_.find(LowerASCIIOnWindows(switch_string)); 310 if (ContainsUpperASCII(switch_string.c_str())) {
311 SwitchMap::const_iterator result =
312 switches_.find(LowerASCIIOnWindows(switch_string));
313 return result == switches_.end() ? StringType() : result->second;
314 }
315 #endif
316
317 SwitchMap::const_iterator result = switches_.find(switch_string);
296 return result == switches_.end() ? StringType() : result->second; 318 return result == switches_.end() ? StringType() : result->second;
297 } 319 }
298 320
299 void CommandLine::AppendSwitch(const std::string& switch_string) { 321 void CommandLine::AppendSwitch(const std::string& switch_string) {
300 AppendSwitchNative(switch_string, StringType()); 322 AppendSwitchNative(switch_string, StringType());
301 } 323 }
302 324
303 void CommandLine::AppendSwitchPath(const std::string& switch_string, 325 void CommandLine::AppendSwitchPath(const std::string& switch_string,
304 const FilePath& path) { 326 const FilePath& path) {
305 AppendSwitchNative(switch_string, path.value()); 327 AppendSwitchNative(switch_string, path.value());
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
446 #if defined(OS_WIN) 468 #if defined(OS_WIN)
447 arg = QuoteForCommandLineToArgvW(arg, quote_placeholders); 469 arg = QuoteForCommandLineToArgvW(arg, quote_placeholders);
448 #endif 470 #endif
449 params.append(arg); 471 params.append(arg);
450 } 472 }
451 } 473 }
452 return params; 474 return params;
453 } 475 }
454 476
455 } // namespace base 477 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | base/command_line_unittest.cc » ('j') | base/command_line_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698