mailing list of musl libc
 help / color / mirror / code / Atom feed
From: <writeonce@midipix.org>
To: musl@lists.openwall.com
Subject: RE: bug in getopt_long()
Date: Sat, 07 Jan 2017 08:13:48 -0700	[thread overview]
Message-ID: <20170107081348.dc30d64f61e5ec441c34ffd4f788e58e.d51391731a.wbe@email15.godaddy.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1266 bytes --]


> 
> 
> -------- Original Message --------
> Subject: [musl] bug in getopt_long()
> From: "Anthony G. Basile" <basile@freeharbor.net>
> Date: Thu, January 05, 2017 9:00 am
> To: musl@lists.openwall.com
> 
> Hi,
> 
> We hit a bug in Gentoo with getopt_long(). It appears the alpine people
> hit it too http://bugs.alpinelinux.org/issues/4802. The problem was
> encountered in open-iscsi but I got some reduced code which I've
> attached. If you build and run that code as `./test --portal
> 10.144.0.14 --discover` then you get
> 
> p
> D
> unrecognized character 'D'
> 
> On a glibc system, you don't get the "unrecognized character 'D'" which
> is what we'd expect. Rich suggested a patch to musl which looked like
> it might work, but it didn't. I didn't investigate further. I'm just
> reporting it first to see if others know what's up.
>

The attached patch should work, I believe; tested with both of the
following:

./a.out --portal 10.144.0.14 --discover
./a.out --portal 10.144.0.14 --discover -- foo bar

midipix

 
> 
> -- 
> Anthony G. Basile, Ph.D.
> Gentoo Linux Developer [Hardened]
> E-Mail : basile@freeharbor.net
> GnuPG FP : 1FED FAD9 D82C 52A5 3BAB DC79 9384 FA6E F52D 4BBA
> GnuPG ID : F52D4BBA
> 


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: getopt_long.patch --]
[-- Type: text/x-diff; name="getopt_long.patch";, Size: 1296 bytes --]

From 36edef5d6b4748bd86575208e7edcffeb124b74f Mon Sep 17 00:00:00 2001
From: midipix <writeonce@midipix.org>
Date: Sat, 7 Jan 2017 09:43:10 -0500
Subject: [PATCH] getopt_long(): record successful completed parsing of all arguments.

---
 src/misc/getopt_long.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/src/misc/getopt_long.c b/src/misc/getopt_long.c
index 568ae7b..5ff0e37 100644
--- a/src/misc/getopt_long.c
+++ b/src/misc/getopt_long.c
@@ -5,6 +5,7 @@
 #include <string.h>
 
 extern int __optpos, __optreset;
+static int __getopt_yay = 0;
 
 static void permute(char *const *argv, int dest, int src)
 {
@@ -26,8 +27,13 @@ static int __getopt_long(int argc, char *const *argv, const char *optstring, con
 	if (!optind || __optreset) {
 		__optreset = 0;
 		__optpos = 0;
+		__getopt_yay = 0;
 		optind = 1;
 	}
+	if (optind == argc && __getopt_yay) {
+		optopt = 0;
+		return -1;
+	}
 	if (optind >= argc || !argv[optind]) return -1;
 	skipped = optind;
 	if (optstring[0] != '+' && optstring[0] != '-') {
@@ -46,6 +52,8 @@ static int __getopt_long(int argc, char *const *argv, const char *optstring, con
 			permute(argv, skipped, optind-1);
 		optind = skipped + cnt;
 	}
+	if ((ret != '?') && (ret != ':'))
+		__getopt_yay = (optind == argc);
 	return ret;
 }
 
-- 
2.8.3


             reply	other threads:[~2017-01-07 15:13 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-07 15:13 writeonce [this message]
  -- strict thread matches above, loose matches on Subject: below --
2017-01-07 16:31 writeonce
2017-01-05 16:00 Anthony G. Basile

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170107081348.dc30d64f61e5ec441c34ffd4f788e58e.d51391731a.wbe@email15.godaddy.com \
    --to=writeonce@midipix.org \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).