Avoid using non-UTF-8 encoded argv more often (flutter/engine#36590)

This commit is contained in:
Dan Field
2022-10-05 12:03:20 -07:00
committed by GitHub
parent 265a01570e
commit e4d709de85
9 changed files with 32 additions and 19 deletions

View File

@@ -14,7 +14,7 @@ namespace benchmarking {
int Main(int argc, char** argv) {
fml::InstallCrashHandler();
#if !defined(FML_OS_ANDROID)
fml::CommandLine cmd = fml::CommandLineFromArgcArgv(argc, argv);
fml::CommandLine cmd = fml::CommandLineFromPlatformOrArgcArgv(argc, argv);
std::string icudtl_path =
cmd.GetOptionValueWithDefault("icu-data-file-path", "icudtl.dat");
fml::icu::InitializeICU(icudtl_path);

View File

@@ -13,7 +13,7 @@
int main(int argc, char** argv) {
fml::InstallCrashHandler();
testing::InitGoogleTest(&argc, argv);
fml::CommandLine cmd = fml::CommandLineFromArgcArgv(argc, argv);
fml::CommandLine cmd = fml::CommandLineFromPlatformOrArgcArgv(argc, argv);
#if defined(OS_FUCHSIA)
flutter::SetGoldenDir(cmd.GetOptionValueWithDefault(

View File

@@ -222,11 +222,32 @@ inline CommandLine CommandLineFromIteratorsWithArgv0(const std::string& argv0,
return builder.Build();
}
// Builds a |CommandLine| by obtaining the arguments of the process using host
// platform APIs. The resulting |CommandLine| will be encoded in UTF-8.
// Returns an empty optional if this is not supported on the host platform.
//
// This can be useful on platforms where argv may not be provided as UTF-8.
std::optional<CommandLine> CommandLineFromPlatform();
// Builds a |CommandLine| from the usual argc/argv.
inline CommandLine CommandLineFromArgcArgv(int argc, const char* const* argv) {
return CommandLineFromIterators(argv, argv + argc);
}
// Builds a |CommandLine| by first trying the platform specific implementation,
// and then falling back to the argc/argv.
//
// If the platform provides a special way of getting arguments, this method may
// discard the values passed in to argc/argv.
inline CommandLine CommandLineFromPlatformOrArgcArgv(int argc,
const char* const* argv) {
auto command_line = CommandLineFromPlatform();
if (command_line.has_value()) {
return *command_line;
}
return CommandLineFromArgcArgv(argc, argv);
}
// Builds a |CommandLine| from an initializer list of |std::string|s or things
// that implicitly convert to |std::string|.
template <typename StringType>
@@ -240,13 +261,6 @@ inline CommandLine CommandLineFromInitializerList(
// outlined at the top of this file.
std::vector<std::string> CommandLineToArgv(const CommandLine& command_line);
// Builds a |CommandLine| by obtaining the arguments of the process using host
// platform APIs. The resulting |CommandLine| will be encoded in UTF-8.
// Returns an empty optional if this is not supported on the host platform.
//
// This can be useful on platforms where argv may not be provided as UTF-8.
std::optional<CommandLine> CommandLineFromPlatform();
} // namespace fml
#endif // LIB_FML_COMMAND_LINE_H_

View File

@@ -49,7 +49,7 @@ bool Main(const fml::CommandLine& command_line) {
} // namespace impeller
int main(int argc, char const* argv[]) {
return impeller::Main(fml::CommandLineFromArgcArgv(argc, argv))
return impeller::Main(fml::CommandLineFromPlatformOrArgcArgv(argc, argv))
? EXIT_SUCCESS
: EXIT_FAILURE;
}

View File

@@ -226,9 +226,8 @@ bool Main(const fml::CommandLine& command_line) {
} // namespace impeller
int main(int argc, char const* argv[]) {
std::optional<fml::CommandLine> command_line = fml::CommandLineFromPlatform();
if (!command_line) {
command_line = fml::CommandLineFromArgcArgv(argc, argv);
}
return impeller::compiler::Main(*command_line) ? EXIT_SUCCESS : EXIT_FAILURE;
return impeller::compiler::Main(
fml::CommandLineFromPlatformOrArgcArgv(argc, argv))
? EXIT_SUCCESS
: EXIT_FAILURE;
}

View File

@@ -372,7 +372,7 @@ int main(int argc, char* argv[]) {
dart::bin::SetExecutableName(argv[0]);
dart::bin::SetExecutableArguments(argc - 1, argv);
auto command_line = fml::CommandLineFromArgcArgv(argc, argv);
auto command_line = fml::CommandLineFromPlatformOrArgcArgv(argc, argv);
if (command_line.HasOption(flutter::FlagForSwitch(flutter::Switch::Help))) {
flutter::PrintUsage("flutter_tester");

View File

@@ -18,7 +18,7 @@
#endif // FML_OS_IOS
std::optional<fml::TimeDelta> GetTestTimeoutFromArgs(int argc, char** argv) {
const auto command_line = fml::CommandLineFromArgcArgv(argc, argv);
const auto command_line = fml::CommandLineFromPlatformOrArgcArgv(argc, argv);
std::string timeout_seconds;
if (!command_line.GetOptionValue("timeout", &timeout_seconds)) {

View File

@@ -23,7 +23,7 @@
// We will use a custom main to allow custom font directories for consistency.
int main(int argc, char** argv) {
::benchmark::Initialize(&argc, argv);
fml::CommandLine cmd = fml::CommandLineFromArgcArgv(argc, argv);
fml::CommandLine cmd = fml::CommandLineFromPlatformOrArgcArgv(argc, argv);
txt::SetCommandLine(cmd);
txt::SetFontDir(flutter::testing::GetFixturesPath());
if (txt::GetFontDir().length() <= 0) {

View File

@@ -26,7 +26,7 @@
int main(int argc, char** argv) {
fml::InstallCrashHandler();
fml::CommandLine cmd = fml::CommandLineFromArgcArgv(argc, argv);
fml::CommandLine cmd = fml::CommandLineFromPlatformOrArgcArgv(argc, argv);
txt::SetCommandLine(cmd);
txt::SetFontDir(flutter::testing::GetFixturesPath());
if (txt::GetFontDir().length() <= 0) {