From 1700cc46b5531499b7b532cda05d442f5f60acd4 Mon Sep 17 00:00:00 2001 From: Tao Bao Date: Mon, 16 Jul 2018 22:09:59 -0700 Subject: Fix the arguments passed to getopt_long(3). The getopt_long(3) implementation in Android (upstream freebsd) expects a null-terminated array while parsing long options with required args. if (long_options[match].has_arg == required_argument) { optarg = nargv[optind++]; } ... if (long_options[match].has_arg == required_argument && optarg == NULL) { return (BADARG); } This seems to make sense in practice, as getopt(3) takes the first two arguments of argc and argv that are "as passed to the main() function on program invocation", and both of C and C++ spec say "the value of argv[argc] shall be 0". Prior to the CL, we may run into undefined behavior on malformed input command line (e.g. missing arg for an option that requires one). This CL fixes the issue by always appending a nullptr to the argument list (but without counting that into argc). Test: Build and boot into recovery with commands. Change-Id: Ic6c37548f4db2f30aeabd40f387ca916eeca5392 --- otautil/sysutil.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'otautil/sysutil.cpp') diff --git a/otautil/sysutil.cpp b/otautil/sysutil.cpp index ab1513088..d8969a0bb 100644 --- a/otautil/sysutil.cpp +++ b/otautil/sysutil.cpp @@ -23,6 +23,7 @@ #include #include +#include #include #include @@ -211,3 +212,11 @@ bool reboot(const std::string& command) { } return android::base::SetProperty(ANDROID_RB_PROPERTY, cmd); } + +std::vector StringVectorToNullTerminatedArray(const std::vector& args) { + std::vector result(args.size()); + std::transform(args.cbegin(), args.cend(), result.begin(), + [](const std::string& arg) { return const_cast(arg.c_str()); }); + result.push_back(nullptr); + return result; +} -- cgit v1.2.3