From patchwork Wed Jul 25 15:26:54 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Sitnicki X-Patchwork-Id: 949281 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=openvswitch.org (client-ip=140.211.169.12; helo=mail.linuxfoundation.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Received: from mail.linuxfoundation.org (mail.linuxfoundation.org [140.211.169.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 41bJxN1XRYz9s0R for ; Thu, 26 Jul 2018 01:27:03 +1000 (AEST) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id 2E08CF38; Wed, 25 Jul 2018 15:27:00 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@mail.linuxfoundation.org Received: from smtp1.linuxfoundation.org (smtp1.linux-foundation.org [172.17.192.35]) by mail.linuxfoundation.org (Postfix) with ESMTPS id 81B91F0A for ; Wed, 25 Jul 2018 15:26:58 +0000 (UTC) X-Greylist: whitelisted by SQLgrey-1.7.6 Received: from mail-wr1-f65.google.com (mail-wr1-f65.google.com [209.85.221.65]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id 0E2A076B for ; Wed, 25 Jul 2018 15:26:57 +0000 (UTC) Received: by mail-wr1-f65.google.com with SMTP id r16-v6so7781296wrt.11 for ; Wed, 25 Jul 2018 08:26:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=clk+fhWhmJyhJbo0rtJmdN6pa8oQYLMBGD+ergd5n+c=; b=tBW+pUkQf76r7Wf2nYhPkoZw10wr+ZtZgaWqa/qZ+7pHs+X8iw7jjd79dNVfO4lVTr SsN0hGT6wfc8peJc9qf76W+ukQsxiNUfqFOBGAsjPAO8msg/V7xP7Y1JtKu+SJrFRQKz qSUoGVcQfFZALvCT+O2g6sJ+L2nC0EFME70FWiedXZBX9qyiueVYqLZGwG0SF8X9ytB4 ilZn2ZRPWTmy1XP3vY/LpQeP29LOkznHXTDEp7k0gxjzi9bJYevP6I8ztHaT22J5xrFP cEzW6PI6/zZFAqyAKBJaeYKI3LJm5YwyBcW/Fi3Dyq/zeKFkZcnO8YfyGa6jkk8r+Xjn X8/w== X-Gm-Message-State: AOUpUlH8ulV112pneEk07O1qzVimyzJC20+7YfEKR8RGMzn6iYBN38Za HGSACfJMd3Fo9Z//6cFReiCYjK1OkCE= X-Google-Smtp-Source: AAOMgpe3JnL15f1oAVgg/Bq1z+fZGvfLI+veX6Hbr2mh+kcWaEvC1JT8QLQ9b1HzrL0fN3zVr7L39Q== X-Received: by 2002:adf:aa0b:: with SMTP id p11-v6mr14352268wrd.220.1532532416346; Wed, 25 Jul 2018 08:26:56 -0700 (PDT) Received: from redhat.com (red-hat-inc.vlan404.asr1.mad1.gblx.net. [64.215.113.190]) by smtp.gmail.com with ESMTPSA id j131-v6sm6235085wmb.25.2018.07.25.08.26.55 for (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 25 Jul 2018 08:26:55 -0700 (PDT) From: Jakub Sitnicki To: dev@openvswitch.org Date: Wed, 25 Jul 2018 17:26:54 +0200 Message-Id: <20180725152654.18068-1-jkbs@redhat.com> X-Mailer: git-send-email 2.14.4 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on smtp1.linux-foundation.org Subject: [ovs-dev] [PATCH] ovn-nbctl: Detect unrecognized short options in server mode. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: ovs-dev-bounces@openvswitch.org Errors-To: ovs-dev-bounces@openvswitch.org Because getopt() will set optopt for both known and unknown options, we need to differentiate between them ourselves by checking if we know the option. Do that by looking up its value. Also, because we are using GNU extensions to getopt(), we need to be resetting getopt() state by setting optind to 0 instead of 1 as pointed out in NOTES in getopt(3) man-page. Not doing so results in invalid reads and optopt being set to a garbarge value. Fixes: 3ec06ea9c668 ("ovn-nbctl: Initial support for daemon mode.") Signed-off-by: Jakub Sitnicki --- ovn/utilities/ovn-nbctl.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/ovn/utilities/ovn-nbctl.c b/ovn/utilities/ovn-nbctl.c index 8b888294e..a1717d153 100644 --- a/ovn/utilities/ovn-nbctl.c +++ b/ovn/utilities/ovn-nbctl.c @@ -4844,6 +4844,19 @@ nbctl_cmd_init(void) ctl_register_commands(nbctl_commands); } +static const struct option * +find_option_by_value(const struct option *options, int value) +{ + const struct option *o; + + for (o = options; o->name; o++) { + if (o->val == value) { + return o; + } + } + return NULL; +} + static char * OVS_WARN_UNUSED_RESULT server_parse_options(int argc, char *argv[], struct shash *local_options, int *n_options_p) @@ -4863,7 +4876,7 @@ server_parse_options(int argc, char *argv[], struct shash *local_options, short_options = build_short_options(global_long_options, false); options = append_command_options(global_long_options, OPT_LOCAL); - optind = 1; + optind = 0; opterr = 0; for (;;) { int idx; @@ -4899,9 +4912,11 @@ server_parse_options(int argc, char *argv[], struct shash *local_options, TABLE_OPTION_HANDLERS(&table_style) case '?': - if (optopt) { + if (find_option_by_value(options, optopt)) { error = xasprintf("option '%s' doesn't allow an argument", argv[optind-1]); + } else if (optopt) { + error = xasprintf("unrecognized option '%c'", optopt); } else { error = xasprintf("unrecognized option '%s'", argv[optind-1]); }