From patchwork Sun Dec 30 18:09:12 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Richard W.M. Jones" X-Patchwork-Id: 10745175 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 64AFE13BF for ; Sun, 30 Dec 2018 18:10:37 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4A5EA28AF2 for ; Sun, 30 Dec 2018 18:10:37 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 3C5C428AF9; Sun, 30 Dec 2018 18:10:37 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id C2DA228AF2 for ; Sun, 30 Dec 2018 18:10:36 +0000 (UTC) Received: from localhost ([127.0.0.1]:45797 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gdfXn-0005kn-Cl for patchwork-qemu-devel@patchwork.kernel.org; Sun, 30 Dec 2018 13:10:35 -0500 Received: from eggs.gnu.org ([208.118.235.92]:44449) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1gdfWj-0004oj-4p for qemu-devel@nongnu.org; Sun, 30 Dec 2018 13:09:29 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1gdfWi-000153-E6 for qemu-devel@nongnu.org; Sun, 30 Dec 2018 13:09:29 -0500 Received: from mx1.redhat.com ([209.132.183.28]:41390) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1gdfWb-0000uQ-6t; Sun, 30 Dec 2018 13:09:21 -0500 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id E689B2D7E6; Sun, 30 Dec 2018 18:09:16 +0000 (UTC) Received: from foo.home.annexia.org (ovpn-116-21.ams2.redhat.com [10.36.116.21]) by smtp.corp.redhat.com (Postfix) with ESMTP id B880A1A92D; Sun, 30 Dec 2018 18:09:15 +0000 (UTC) From: "Richard W.M. Jones" To: eblake@redhat.com Date: Sun, 30 Dec 2018 18:09:12 +0000 Message-Id: <20181230180912.14614-2-rjones@redhat.com> In-Reply-To: <20181230180912.14614-1-rjones@redhat.com> References: <20181230180912.14614-1-rjones@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Sun, 30 Dec 2018 18:09:16 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [PATCH] qemu-io: Reinitialize optind correctly before parsing inner command. X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, qemu-devel@nongnu.org, qemu-block@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP On FreeBSD 11.2: $ ./qemu-io -f raw -c "aio_write 0 512" "nbd:localhost:10809" Parsing error: non-numeric argument, or extraneous/unrecognized suffix -- aio_write After main option parsing, we reinitialize optind so we can parse each command. The error happens when parsing the aio_write command. After the aio_write getopt loop, optind == 0 and argv[optind] points to the command name ("aio_write" in this case). The code fails because it tries to parse argv[optind] (which it thinks is the first argument) as an integer. In fact optind _starts_ the loop as 0, because we set it to 0. 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. (From the rest of the man page it is clear that optind must be reinitialized to 1). Unfortunately this conflicts with the glibc man page which says: A program that scans multiple argument vectors, or rescans the same vector more than once, and wants to make use of GNU extensions such as '+' and '-' at the start of optstring, or changes the value of POSIXLY_CORRECT between scans, must reinitialize getopt() by resetting optind to 0, rather than the traditional value of 1. (Resetting to 0 forces the invocation of an internal initialization routine that rechecks POSIXLY_CORRECT and checks for GNU extensions in optstring.) Reinitialize optind to either 0 or 1 depending on whether we're using glibc or not. I didn't set optreset - it's not present in glibc and it doesn't seem to make any difference on FreeBSD. Signed-off-by: Richard W.M. Jones --- qemu-io-cmds.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/qemu-io-cmds.c b/qemu-io-cmds.c index 2c39124036..ca4e258579 100644 --- a/qemu-io-cmds.c +++ b/qemu-io-cmds.c @@ -114,7 +114,11 @@ static int command(BlockBackend *blk, const cmdinfo_t *ct, int argc, } } +#ifdef __GNU_LIBRARY__ optind = 0; +#else + optind = 1; +#endif return ct->cfunc(blk, argc, argv); }