fix: hard crash on invalid command line switches (#46004)

* fix: hard crash on invalid command line switch

* Update docs/api/command-line.md

Co-authored-by: Niklas Wenzel <dev@nikwen.de>

* chore: feedback from review

* docs: Add breaking change note

---------

Co-authored-by: Niklas Wenzel <dev@nikwen.de>
This commit is contained in:
Shelley Vohr 2025-03-26 14:14:03 +01:00 committed by GitHub
parent 8412d78310
commit d2c2261c58
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 75 additions and 16 deletions

View file

@ -4,50 +4,59 @@
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/strings/string_util.h"
#include "services/network/public/cpp/network_switches.h"
#include "shell/common/gin_converters/base_converter.h"
#include "shell/common/gin_converters/file_path_converter.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/node_includes.h"
#include "third_party/abseil-cpp/absl/strings/ascii.h"
namespace {
bool HasSwitch(const std::string& switch_string) {
auto switch_str = base::ToLowerASCII(switch_string);
bool HasSwitch(const std::string& name) {
return base::CommandLine::ForCurrentProcess()->HasSwitch(name);
auto* command_line = base::CommandLine::ForCurrentProcess();
return command_line->HasSwitch(switch_str);
}
base::CommandLine::StringType GetSwitchValue(const std::string& name) {
return base::CommandLine::ForCurrentProcess()->GetSwitchValueNative(name);
base::CommandLine::StringType GetSwitchValue(gin_helper::ErrorThrower thrower,
const std::string& switch_string) {
auto switch_str = base::ToLowerASCII(switch_string);
auto* command_line = base::CommandLine::ForCurrentProcess();
return command_line->GetSwitchValueNative(switch_str);
}
void AppendSwitch(const std::string& switch_string,
gin_helper::Arguments* args) {
auto switch_str = base::ToLowerASCII(switch_string);
auto* command_line = base::CommandLine::ForCurrentProcess();
if (base::EndsWith(switch_string, "-path",
base::CompareCase::INSENSITIVE_ASCII) ||
switch_string == network::switches::kLogNetLog) {
base::FilePath path;
args->GetNext(&path);
command_line->AppendSwitchPath(switch_string, path);
command_line->AppendSwitchPath(switch_str, path);
return;
}
base::CommandLine::StringType value;
if (args->GetNext(&value))
command_line->AppendSwitchNative(switch_string, value);
command_line->AppendSwitchNative(switch_str, value);
else
command_line->AppendSwitch(switch_string);
command_line->AppendSwitch(switch_str);
}
void RemoveSwitch(const std::string& switch_string) {
auto switch_str = base::ToLowerASCII(switch_string);
auto* command_line = base::CommandLine::ForCurrentProcess();
command_line->RemoveSwitch(switch_string);
command_line->RemoveSwitch(switch_str);
}
void AppendArg(const std::string& arg) {
auto* command_line = base::CommandLine::ForCurrentProcess();
command_line->AppendArg(arg);
}