diff options
author | Karel Zak <kzak@redhat.com> | 2016-10-21 13:51:48 +0200 |
---|---|---|
committer | Karel Zak <kzak@redhat.com> | 2016-10-21 13:51:48 +0200 |
commit | e1164591f7927402af8d73d340e75dbfeb06a288 (patch) | |
tree | 2b36fb5845ef5e6ab5b52a01174b1479317dd744 | |
parent | 25d08572da2b950c14a4820f5d581eaba957361f (diff) | |
download | util-linux-e1164591f7927402af8d73d340e75dbfeb06a288.tar.gz |
getopt: be sure that options array is terminated
Reported-by: Anders Kaseorg <andersk@mit.edu>
Signed-off-by: Karel Zak <kzak@redhat.com>
-rw-r--r-- | misc-utils/getopt.c | 19 |
1 files changed, 11 insertions, 8 deletions
diff --git a/misc-utils/getopt.c b/misc-utils/getopt.c index 01b58832b7..94cfec5ad5 100644 --- a/misc-utils/getopt.c +++ b/misc-utils/getopt.c @@ -253,6 +253,7 @@ static void __attribute__ ((__noreturn__)) parse_error(const char *message) static void add_longopt(struct getopt_control *ctl, const char *name, int has_arg) { static int flag; + int nr = ctl->long_options_nr; if (ctl->long_options_nr == ctl->long_options_length) { ctl->long_options_length += REALLOC_INCREMENT; @@ -262,10 +263,16 @@ static void add_longopt(struct getopt_control *ctl, const char *name, int has_ar } if (name) { /* Not for init! */ - ctl->long_options[ctl->long_options_nr].has_arg = has_arg; - ctl->long_options[ctl->long_options_nr].flag = &flag; - ctl->long_options[ctl->long_options_nr].val = ctl->long_options_nr; - ctl->long_options[ctl->long_options_nr].name = xstrdup(name); + ctl->long_options[nr].has_arg = has_arg; + ctl->long_options[nr].flag = &flag; + ctl->long_options[nr].val = ctl->long_options_nr; + ctl->long_options[nr].name = xstrdup(name); + } else { + /* lets use add_longopt(ct, NULL, 0) to terminate the array */ + ctl->long_options[nr].name = NULL; + ctl->long_options[nr].has_arg = 0; + ctl->long_options[nr].flag = NULL; + ctl->long_options[nr].val = 0; } } @@ -303,10 +310,6 @@ static void add_long_options(struct getopt_control *ctl, char *options) tokptr = strtok(NULL, ", \t\n"); } add_longopt(ctl, NULL, 0); /* ensure long_options[] is not full */ - ctl->long_options[ctl->long_options_nr].name = NULL; - ctl->long_options[ctl->long_options_nr].has_arg = 0; - ctl->long_options[ctl->long_options_nr].flag = NULL; - ctl->long_options[ctl->long_options_nr].val = 0; } static shell_t shell_type(const char *new_shell) |