aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Frysinger <vapier@google.com>2018-02-05 13:39:39 -0500
committerMike Frysinger <vapier@google.com>2018-02-05 13:39:39 -0500
commit9741372f23e7895a180a535c9bfd7246bb69dd2b (patch)
treee12ffa400796237bd2e5c0c5d04005ac15733875
parent4f3e09f23a0e338440513c0e10db14d41ebc5dd0 (diff)
downloadminijail-9741372f23e7895a180a535c9bfd7246bb69dd2b.tar.gz
minijail0_cli_unittest: fix random crashes/failures
The GNU getopt API cannot handle being passed different argv vectors by default. Changing optind back to 1 will trigger a reset of state, but only relative to the previous argv getopt saw. Instead, optind has to be set to 0 so all internal state is reset. This extension is from GNU (glibc), but Android's bionic also supports it. Otherwise, when we pass in a different argv, the internal state might randomly refer to the old argv memory which in turn can lead to random errors or memory violations. Bug: None Test: `./minijail0_cli_unittest --gtest_repeat=-1` no longer randomly crashes Change-Id: I79276518bb51e297719049c7efa9824d6f97c7ab
-rw-r--r--minijail0_cli_unittest.cc6
1 files changed, 4 insertions, 2 deletions
diff --git a/minijail0_cli_unittest.cc b/minijail0_cli_unittest.cc
index a774d55..2427a01 100644
--- a/minijail0_cli_unittest.cc
+++ b/minijail0_cli_unittest.cc
@@ -42,8 +42,10 @@ class CliTest : public ::testing::Test {
// as it parses things (which is normally permissible with argv).
int parse_args_(const std::vector<std::string>& argv, int *exit_immediately,
ElfType *elftype) {
- // Make sure we reset the getopts state when scanning a new argv.
- optind = 1;
+ // Make sure we reset the getopts state when scanning a new argv. Setting
+ // this to 0 is a GNU extension, but AOSP/BSD also checks this (as an alias
+ // to their "optreset").
+ optind = 0;
std::vector<const char *> pargv;
pargv.push_back("minijail0");