From ef9c12b157a50d63e8a8eb710c013d16c2cea319 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 21 Mar 2023 10:05:33 +0900 Subject: [PATCH] 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. --- src/activate/activate.c | 3 +++ src/ask-password/ask-password.c | 4 ++++ src/cgls/cgls.c | 3 +++ src/journal/cat.c | 3 +++ src/login/inhibit.c | 3 +++ src/machine/machinectl.c | 4 ++++ src/nspawn/nspawn.c | 3 +++ src/run/run.c | 3 +++ src/shutdown/shutdown.c | 4 ++++ src/udev/udevadm-lock.c | 3 +++ src/udev/udevadm.c | 3 +++ src/userdb/userdbctl.c | 4 ++++ 12 files changed, 40 insertions(+) diff --git a/src/activate/activate.c b/src/activate/activate.c index 4a639703260..1caa30d7d47 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 f161e659730..e9c09b78252 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 32780c606a8..a34b0aa604b 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 5908758a8f0..d3f7785ad34 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 7cd2fd3e668..25ba848492d 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 6a29a32bcd2..447f1a70b0a 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 982dffd1b89..75349c3b0ec 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 8377c2e8cdd..ee4a151a4ab 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 42111d27728..5dee1b3a926 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 d19e7561f81..6d8a5c336f5 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 30a72f2a429..b803f7bb0f5 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 eab0c3af151..67675b4b7f7 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; -- 2.47.3