On 18/02/15 03:50, Pádraig Brady wrote: > On 16/02/15 21:55, Tobias Stoeckmann wrote: >> On Mon, Feb 16, 2015 at 08:57:00PM +0000, Pádraig Brady wrote: >>> I'm not sure it's generally worth proceeding in OOM conditions >>> unless there are clear fall backs. It would be nice to depend on xalloc >>> and just call xmalloc(). Could this suffice? >>> >>> #ifdef _LIBC >>> struct option_list *newp = alloca (sizeof (*newp)); >>> #else >>> struct option_list *newp = xmalloc (sizeof (*newp)); >>> #endif >> >> Yeah, I am fine with that solution. >> Didn't think about simply using xmalloc there. Maybe it was too easy. :) > > We can't use xmalloc for licensing reasons. > Anyway it's better for this lib to fall back to a degraded diagnostic than > exit. > Also I noticed a mem leak in the non mem exhaustion case. > Hopefully the attached addresses all this.
I amended this to break out of the option matching loop upon memory exhaustion and pushed, but then realised that this could give a false indication of ambiguous options. So I pushed the following as well. thanks, Pádraig. >From 8e841aec83c3b75013493e677bc53cc907995140 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C3=A1draig=20Brady?= <p...@draigbrady.com> Date: Wed, 18 Feb 2015 22:37:31 +0000 Subject: [PATCH] getopt: give accurate ambiguity diagnostic on mem exhaustion * lib/getopt.c (_getopt_internal_r): The previous commit broke out the loop too early, which could give a false indication of ambiguous options under memory exhaustion. --- lib/getopt.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/getopt.c b/lib/getopt.c index 6474ba7..212cbf7 100644 --- a/lib/getopt.c +++ b/lib/getopt.c @@ -527,6 +527,8 @@ _getopt_internal_r (int argc, char **argv, const char *optstring, pfound = p; indfound = option_index; } + else if (ambig) + ; /* Taking simpler path to handling ambiguities. */ else if (long_only || pfound->has_arg != p->has_arg || pfound->flag != p->flag @@ -539,10 +541,9 @@ _getopt_internal_r (int argc, char **argv, const char *optstring, struct option_list *newp = malloc (sizeof (*newp)); if (newp == NULL) { - ambig = 1; /* Use simpler fallback message. */ free_option_list (ambig_list); ambig_list = NULL; - break; + ambig = 1; /* Use simpler fallback message. */ } else #endif -- 2.1.0