Skip to content

Commit 0000800

Browse files
yuwatapull[bot]
authored andcommitted
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.
1 parent 0d7d303 commit 0000800

File tree

12 files changed

+40
-0
lines changed

12 files changed

+40
-0
lines changed

src/activate/activate.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,9 @@ static int parse_argv(int argc, char *argv[]) {
347347
assert(argc >= 0);
348348
assert(argv);
349349

350+
/* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
351+
* that checks for GNU extensions in optstring ('-' or '+' at the beginning). */
352+
optind = 0;
350353
while ((c = getopt_long(argc, argv, "+hl:aE:d", options, NULL)) >= 0)
351354
switch (c) {
352355
case 'h':

src/ask-password/ask-password.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ static int parse_argv(int argc, char *argv[]) {
108108

109109
/* Note the asymmetry: the long option --echo= allows an optional argument, the short option does
110110
* not. */
111+
112+
/* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
113+
* that checks for GNU extensions in optstring ('-' or '+' at the beginning). */
114+
optind = 0;
111115
while ((c = getopt_long(argc, argv, "+hen", options, NULL)) >= 0)
112116

113117
switch (c) {

src/cgls/cgls.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ static int parse_argv(int argc, char *argv[]) {
9393
assert(argc >= 1);
9494
assert(argv);
9595

96+
/* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
97+
* that checks for GNU extensions in optstring ('-' or '+' at the beginning). */
98+
optind = 0;
9699
while ((c = getopt_long(argc, argv, "-hkalM:u::xc", options, NULL)) >= 0)
97100

98101
switch (c) {

src/journal/cat.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ static int parse_argv(int argc, char *argv[]) {
7575
assert(argc >= 0);
7676
assert(argv);
7777

78+
/* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
79+
* that checks for GNU extensions in optstring ('-' or '+' at the beginning). */
80+
optind = 0;
7881
while ((c = getopt_long(argc, argv, "+ht:p:", options, NULL)) >= 0)
7982

8083
switch (c) {

src/login/inhibit.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,9 @@ static int parse_argv(int argc, char *argv[]) {
210210
assert(argc >= 0);
211211
assert(argv);
212212

213+
/* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
214+
* that checks for GNU extensions in optstring ('-' or '+' at the beginning). */
215+
optind = 0;
213216
while ((c = getopt_long(argc, argv, "+h", options, NULL)) >= 0)
214217

215218
switch (c) {

src/machine/machinectl.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2720,6 +2720,10 @@ static int parse_argv(int argc, char *argv[]) {
27202720
assert(argc >= 0);
27212721
assert(argv);
27222722

2723+
/* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
2724+
* that checks for GNU extensions in optstring ('-' or '+' at the beginning). */
2725+
optind = 0;
2726+
27232727
for (;;) {
27242728
static const char option_string[] = "-hp:als:H:M:qn:o:E:";
27252729

src/nspawn/nspawn.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,9 @@ static int parse_argv(int argc, char *argv[]) {
815815
assert(argc >= 0);
816816
assert(argv);
817817

818+
/* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
819+
* that checks for GNU extensions in optstring ('-' or '+' at the beginning). */
820+
optind = 0;
818821
while ((c = getopt_long(argc, argv, "+hD:u:abL:M:jS:Z:qi:xp:nUE:P", options, NULL)) >= 0)
819822
switch (c) {
820823

src/run/run.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ static int parse_argv(int argc, char *argv[]) {
242242
assert(argc >= 0);
243243
assert(argv);
244244

245+
/* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
246+
* that checks for GNU extensions in optstring ('-' or '+' at the beginning). */
247+
optind = 0;
245248
while ((c = getopt_long(argc, argv, "+hrH:M:E:p:tPqGdSu:", options, NULL)) >= 0)
246249

247250
switch (c) {

src/shutdown/shutdown.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ static int parse_argv(int argc, char *argv[]) {
7575
assert(argc >= 1);
7676
assert(argv);
7777

78+
/* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
79+
* that checks for GNU extensions in optstring ('-' or '+' at the beginning). */
80+
optind = 0;
81+
7882
/* "-" prevents getopt from permuting argv[] and moving the verb away
7983
* from argv[1]. Our interface to initrd promises it'll be there. */
8084
while ((c = getopt_long(argc, argv, "-", options, NULL)) >= 0)

src/udev/udevadm-lock.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ static int parse_argv(int argc, char *argv[]) {
7575
assert(argc >= 0);
7676
assert(argv);
7777

78+
/* Resetting to 0 forces the invocation of an internal initialization routine of getopt_long()
79+
* that checks for GNU extensions in optstring ('-' or '+' at the beginning). */
80+
optind = 0;
7881
while ((c = getopt_long(argc, argv, arg_print ? "hVd:b:t:p" : "+hVd:b:t:p", options, NULL)) >= 0)
7982

8083
switch (c) {

0 commit comments

Comments
 (0)