All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] getopt: Terminate long_options even if add_long_options is never called
@ 2016-10-19 22:35 Anders Kaseorg
  2016-10-21 12:12 ` Karel Zak
  0 siblings, 1 reply; 3+ messages in thread
From: Anders Kaseorg @ 2016-10-19 22:35 UTC (permalink / raw)
  To: util-linux; +Cc: Sami Kerola

v2.26-rc1~140^2 “getopt: avoid re-terminating long_option list at
every update” forgot to terminate the long_options list in the case
where -l/--longoptions was not specified at all, so that
add_long_options is never called.  The resulting garbage may crash
getopt if any long options are provided:

$ LANG=de_DE getopt -o c -- --help
Segmentation fault (core dumped)

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
---
 misc-utils/getopt.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/misc-utils/getopt.c b/misc-utils/getopt.c
index 01b5883..08e92c0 100644
--- a/misc-utils/getopt.c
+++ b/misc-utils/getopt.c
@@ -397,6 +397,10 @@ int main(int argc, char *argv[])
 	}
 
 	add_longopt(&ctl, NULL, 0);	/* init */
+	ctl.long_options[0].name = NULL;
+	ctl.long_options[0].has_arg = 0;
+	ctl.long_options[0].flag = NULL;
+	ctl.long_options[0].val = 0;
 	getopt_long_fp = getopt_long;
 
 	if (argv[1][0] != '-' || ctl.compatible) {
-- 
2.10.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] getopt: Terminate long_options even if add_long_options is never called
  2016-10-19 22:35 [PATCH] getopt: Terminate long_options even if add_long_options is never called Anders Kaseorg
@ 2016-10-21 12:12 ` Karel Zak
  2016-10-21 19:26   ` Anders Kaseorg
  0 siblings, 1 reply; 3+ messages in thread
From: Karel Zak @ 2016-10-21 12:12 UTC (permalink / raw)
  To: Anders Kaseorg; +Cc: util-linux, Sami Kerola

On Wed, Oct 19, 2016 at 06:35:57PM -0400, Anders Kaseorg wrote:
> v2.26-rc1~140^2 “getopt: avoid re-terminating long_option list at
> every update” forgot to terminate the long_options list in the case
> where -l/--longoptions was not specified at all, so that
> add_long_options is never called.  The resulting garbage may crash
> getopt if any long options are provided:
> 
> $ LANG=de_DE getopt -o c -- --help
> Segmentation fault (core dumped)

I have applied a little bit different solution
https://github.com/karelzak/util-linux/commit/e1164591f7927402af8d73d340e75dbfeb06a288

> +	ctl.long_options[0].name = NULL;
> +	ctl.long_options[0].has_arg = 0;
> +	ctl.long_options[0].flag = NULL;
> +	ctl.long_options[0].val = 0;

now add_longopt(ctl, NULL, 0) always terminates the array.

Please, re-test it. Thanks!

    Karel

-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] getopt: Terminate long_options even if add_long_options is never called
  2016-10-21 12:12 ` Karel Zak
@ 2016-10-21 19:26   ` Anders Kaseorg
  0 siblings, 0 replies; 3+ messages in thread
From: Anders Kaseorg @ 2016-10-21 19:26 UTC (permalink / raw)
  To: Karel Zak; +Cc: util-linux, Sami Kerola

On Fri, 21 Oct 2016, Karel Zak wrote:
> I have applied a little bit different solution
> https://github.com/karelzak/util-linux/commit/e1164591f7927402af8d73d340e75dbfeb06a288
> 
> now add_longopt(ctl, NULL, 0) always terminates the array.
> 
> Please, re-test it. Thanks!

Works for me.

Anders

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-10-21 19:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-10-19 22:35 [PATCH] getopt: Terminate long_options even if add_long_options is never called Anders Kaseorg
2016-10-21 12:12 ` Karel Zak
2016-10-21 19:26   ` Anders Kaseorg

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.