From: Yu Watanabe Date: Tue, 21 Mar 2023 01:05:33 +0000 (+0900) Subject: tree-wide: reset optind to 0 when GNU extensions in optstring are used X-Git-Tag: v254-rc1~787^2~1 X-Git-Url: http://git-history.diyao.me/?a=commitdiff_plain;h=ef9c12b157a50d63e8a8eb710c013d16c2cea319;p=systemd%2F.git tree-wide: reset optind to 0 when GNU extensions in optstring are used Otherwise, if getopt() and friends are used before parse_argv(), then the GNU extensions may be ignored. This should not change any behavior at least now, as we usually use getopt_long() only once per invocation. But in the next commit, getopt_long() will be used for other arrays, hence this change will become necessary. --- diff --git a/src/activate/activate.c b/src/activate/activate.c index 4a63970326..1caa30d7d4 100644 --- a/src/activate/activate.c +++ b/src/activate/activate.c @@ -347,6 +347,9 @@ static int parse_argv(int argc, char *argv[]) { assert(argc >= 0); assert(argv); + /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long() + * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */ + optind = 0; while ((c = getopt_long(argc, argv, "+hl:aE:d", options, NULL)) >= 0) switch (c) { case 'h': diff --git a/src/ask-password/ask-password.c b/src/ask-password/ask-password.c index f161e65973..e9c09b7825 100644 --- a/src/ask-password/ask-password.c +++ b/src/ask-password/ask-password.c @@ -108,6 +108,10 @@ static int parse_argv(int argc, char *argv[]) { /* Note the asymmetry: the long option --echo= allows an optional argument, the short option does * not. */ + + /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long() + * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */ + optind = 0; while ((c = getopt_long(argc, argv, "+hen", options, NULL)) >= 0) switch (c) { diff --git a/src/cgls/cgls.c b/src/cgls/cgls.c index 32780c606a..a34b0aa604 100644 --- a/src/cgls/cgls.c +++ b/src/cgls/cgls.c @@ -93,6 +93,9 @@ static int parse_argv(int argc, char *argv[]) { assert(argc >= 1); assert(argv); + /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long() + * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */ + optind = 0; while ((c = getopt_long(argc, argv, "-hkalM:u::xc", options, NULL)) >= 0) switch (c) { diff --git a/src/journal/cat.c b/src/journal/cat.c index 5908758a8f..d3f7785ad3 100644 --- a/src/journal/cat.c +++ b/src/journal/cat.c @@ -75,6 +75,9 @@ static int parse_argv(int argc, char *argv[]) { assert(argc >= 0); assert(argv); + /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long() + * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */ + optind = 0; while ((c = getopt_long(argc, argv, "+ht:p:", options, NULL)) >= 0) switch (c) { diff --git a/src/login/inhibit.c b/src/login/inhibit.c index 7cd2fd3e66..25ba848492 100644 --- a/src/login/inhibit.c +++ b/src/login/inhibit.c @@ -210,6 +210,9 @@ static int parse_argv(int argc, char *argv[]) { assert(argc >= 0); assert(argv); + /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long() + * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */ + optind = 0; while ((c = getopt_long(argc, argv, "+h", options, NULL)) >= 0) switch (c) { diff --git a/src/machine/machinectl.c b/src/machine/machinectl.c index 6a29a32bcd..447f1a70b0 100644 --- a/src/machine/machinectl.c +++ b/src/machine/machinectl.c @@ -2720,6 +2720,10 @@ static int parse_argv(int argc, char *argv[]) { assert(argc >= 0); assert(argv); + /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long() + * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */ + optind = 0; + for (;;) { static const char option_string[] = "-hp:als:H:M:qn:o:E:"; diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c index 982dffd1b8..75349c3b0e 100644 --- a/src/nspawn/nspawn.c +++ b/src/nspawn/nspawn.c @@ -815,6 +815,9 @@ static int parse_argv(int argc, char *argv[]) { assert(argc >= 0); assert(argv); + /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long() + * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */ + optind = 0; while ((c = getopt_long(argc, argv, "+hD:u:abL:M:jS:Z:qi:xp:nUE:P", options, NULL)) >= 0) switch (c) { diff --git a/src/run/run.c b/src/run/run.c index 8377c2e8cd..ee4a151a4a 100644 --- a/src/run/run.c +++ b/src/run/run.c @@ -242,6 +242,9 @@ static int parse_argv(int argc, char *argv[]) { assert(argc >= 0); assert(argv); + /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long() + * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */ + optind = 0; while ((c = getopt_long(argc, argv, "+hrH:M:E:p:tPqGdSu:", options, NULL)) >= 0) switch (c) { diff --git a/src/shutdown/shutdown.c b/src/shutdown/shutdown.c index 42111d2772..5dee1b3a92 100644 --- a/src/shutdown/shutdown.c +++ b/src/shutdown/shutdown.c @@ -75,6 +75,10 @@ static int parse_argv(int argc, char *argv[]) { assert(argc >= 1); assert(argv); + /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long() + * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */ + optind = 0; + /* "-" prevents getopt from permuting argv[] and moving the verb away * from argv[1]. Our interface to initrd promises it'll be there. */ while ((c = getopt_long(argc, argv, "-", options, NULL)) >= 0) diff --git a/src/udev/udevadm-lock.c b/src/udev/udevadm-lock.c index d19e7561f8..6d8a5c336f 100644 --- a/src/udev/udevadm-lock.c +++ b/src/udev/udevadm-lock.c @@ -75,6 +75,9 @@ static int parse_argv(int argc, char *argv[]) { assert(argc >= 0); assert(argv); + /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long() + * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */ + optind = 0; while ((c = getopt_long(argc, argv, arg_print ? "hVd:b:t:p" : "+hVd:b:t:p", options, NULL)) >= 0) switch (c) { diff --git a/src/udev/udevadm.c b/src/udev/udevadm.c index 30a72f2a42..b803f7bb0f 100644 --- a/src/udev/udevadm.c +++ b/src/udev/udevadm.c @@ -62,6 +62,9 @@ static int parse_argv(int argc, char *argv[]) { assert(argc >= 0); assert(argv); + /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long() + * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */ + optind = 0; while ((c = getopt_long(argc, argv, "+dhV", options, NULL)) >= 0) switch (c) { diff --git a/src/userdb/userdbctl.c b/src/userdb/userdbctl.c index eab0c3af15..67675b4b7f 100644 --- a/src/userdb/userdbctl.c +++ b/src/userdb/userdbctl.c @@ -1150,6 +1150,10 @@ static int parse_argv(int argc, char *argv[]) { arg_services = l; } + /* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long() + * that checks for GNU extensions in optstring ('-' or '+' at the beginning). */ + optind = 0; + for (;;) { int c;