From: Eric Blake <eblake@redhat.com>
To: Max Reitz <mreitz@redhat.com>, "Richard W.M. Jones" <rjones@redhat.com>
Cc: kwolf@redhat.com, qemu-block@nongnu.org, qemu-devel@nongnu.org
Subject: Re: [Qemu-devel] [PATCH v2] qemu-io: Reinitialize optind to 1 (not 0) before parsing inner command.
Date: Mon, 7 Jan 2019 11:46:28 -0600 [thread overview]
Message-ID: <741a5843-a47a-6a02-c378-b778f7ffe172@redhat.com> (raw)
In-Reply-To: <cb7b80d8-e54f-5422-7029-47ee286dd25f@redhat.com>
[-- Attachment #1: Type: text/plain, Size: 2660 bytes --]
On 1/7/19 11:17 AM, Max Reitz wrote:
> On 03.01.19 10:47, Richard W.M. Jones wrote:
>> On FreeBSD 11.2:
>>
>> $ nbdkit memory size=1M --run './qemu-io -f raw -c "aio_write 0 512" $nbd'
>> Parsing error: non-numeric argument, or extraneous/unrecognized suffix -- aio_write
>>
>> After main option parsing, we reinitialize optind so we can parse each
>> command. However reinitializing optind to 0 does not work on FreeBSD.
>> What happens when you do this is optind remains 0 after the option
>> parsing loop, and the result is we try to parse argv[optind] ==
>> argv[0] == "aio_write" as if it was the first parameter.
>>
>> The FreeBSD manual page says:
>>
>> In order to use getopt() to evaluate multiple sets of arguments, or to
>> evaluate a single set of arguments multiple times, the variable optreset
>> must be set to 1 before the second and each additional set of calls to
>> getopt(), and the variable optind must be reinitialized.
>
> [...]
>
>> Note I didn't set optreset. It's not present in glibc and the "hard
>> reset" is not necessary in this context.
>
> But it sure sounds like FreeBSD requires you to set it, doesn't it?
The reason BSD and glibc have a hard reset path is because of hidden
state - both BSD and glibc track state that remembers if the options
began with '+' or '-' (both of those are extensions beyond POSIX), and
whether POSIXLY_CORRECT was set. Beyond that hidden state is a corner
case of one more piece of state that you can trigger using only POSIX:
if the user passes './prog -ab' while you had code:
swich (getopt(argc, argv, "ab")) {
case 'a': optind = 1; ...
then things fall apart for both BSD and glibc, because getopt() has to
track invisible state in order to remember that the next call will
process the -b portion of the merged short-option in argv[optind==1]
rather than repeating the -a half and before moving on to optind==2.
But this latter corner case can only happen when getopt() did not return -1.
At the end of the day, both GNU optind=0 and BSD optreset=1 are
sufficient to force a hard reset of all hidden state. But if you don't
use POSIX extensions, and always run getopt() until a -1 return, then
setting optind=1 is a portable soft reset, regardless of how the hidden
state is implemented, and regardless of how (or even if) libc offers a
hard reset, even though POSIX itself is currently lacking that mention.
(I should probably file a POSIX defect to get that wording listed in POSIX)
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3226
Virtualization: qemu.org | libvirt.org
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 488 bytes --]
next prev parent reply other threads:[~2019-01-07 17:53 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-01-03 9:47 [Qemu-devel] [PATCH v2] qemu-io: Reinitialize optind to 1 (not 0) before parsing inner command Richard W.M. Jones
2019-01-03 9:47 ` Richard W.M. Jones
2019-01-03 19:46 ` Eric Blake
2019-01-07 17:17 ` Max Reitz
2019-01-07 17:46 ` Eric Blake [this message]
2019-01-07 17:50 ` Max Reitz
2019-01-07 17:59 ` Eric Blake
2019-01-07 18:14 ` Max Reitz
2019-01-07 18:45 ` Eric Blake
2019-01-09 12:30 ` Max Reitz
2019-01-07 18:40 ` Richard W.M. Jones
2019-01-08 12:16 ` Kevin Wolf
2019-01-08 14:51 ` Eric Blake
2019-01-08 15:13 ` Kevin Wolf
2019-01-08 15:35 ` Richard W.M. Jones
2019-01-09 12:33 ` Max Reitz
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=741a5843-a47a-6a02-c378-b778f7ffe172@redhat.com \
--to=eblake@redhat.com \
--cc=kwolf@redhat.com \
--cc=mreitz@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=rjones@redhat.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.
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).