Skip to content

Commit

Permalink
Avoid using non-UTF-8 encoded argv more often (flutter#36590)
Browse files Browse the repository at this point in the history
  • Loading branch information
dnfield authored Oct 5, 2022
1 parent ef0a757 commit 3ea6871
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 19 deletions.
2 changes: 1 addition & 1 deletion benchmarking/benchmarking.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion flow/flow_run_all_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
28 changes: 21 additions & 7 deletions fml/command_line.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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_
2 changes: 1 addition & 1 deletion impeller/blobcat/blobcat_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
9 changes: 4 additions & 5 deletions impeller/compiler/impellerc_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion shell/testing/tester_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion testing/run_all_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
2 changes: 1 addition & 1 deletion third_party/txt/benchmarks/txt_run_all_benchmarks.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion third_party/txt/tests/txt_run_all_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 3ea6871

Please sign in to comment.