From nobody Sun May 19 03:54:32 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1586446336; cv=none; d=zohomail.com; s=zohoarc; b=ZAt35JrlH9O8UyXLsOUJBJQULeFMifMQu3oEA7P8YauWpujp3Rx+lJDKW9+PxIINWPZbBvs5miARIIAKEraQAxxMN45bIvyBQNUHav1dBze8S8eGHs6rtG47J7zSpvzdVLklq6ig24vUQkksYxIkLTiGCy1aQ3C0L3Hyuh17Y5o= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1586446336; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=/Ic+DfQ5DkGWSU3nvML24mnF+9TJHSI7N3BN4jDm3zE=; b=OMj5N9sxa4M372q9HYL/CWzz17dktTYn3k88pIBaF17nIQy7aKkpxdTJ2ZMV8aDXEucNrfPgVE9Doru0w5zE18IiNkA8HmnYSi/2sJzv2ndeBHBQb1WJ3ByGqNI4bMiJtRtAv49n50gX+QvC2LW38a8HgOz0wmTMs9kA+aufipU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1586446336174698.2940978241402; Thu, 9 Apr 2020 08:32:16 -0700 (PDT) Received: from localhost ([::1]:51754 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jMZA6-0002qf-FC for importer@patchew.org; Thu, 09 Apr 2020 11:32:14 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50428) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jMZ8i-00016C-4m for qemu-devel@nongnu.org; Thu, 09 Apr 2020 11:30:49 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jMZ8g-0003cF-Pz for qemu-devel@nongnu.org; Thu, 09 Apr 2020 11:30:47 -0400 Received: from us-smtp-2.mimecast.com ([207.211.31.81]:28299 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jMZ8g-0003aq-M9 for qemu-devel@nongnu.org; Thu, 09 Apr 2020 11:30:46 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-199-EF5HrFwLN8mQVnXpHYRH4A-1; Thu, 09 Apr 2020 11:30:43 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E5FFC1005509; Thu, 9 Apr 2020 15:30:42 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-113-20.ams2.redhat.com [10.36.113.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B023D5C296; Thu, 9 Apr 2020 15:30:42 +0000 (UTC) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id 3640211384A9; Thu, 9 Apr 2020 17:30:41 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1586446245; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=/Ic+DfQ5DkGWSU3nvML24mnF+9TJHSI7N3BN4jDm3zE=; b=ZtjQUi91VRH/qyB2WvcyJysaz4VqaldG31VYaCU/crCFijf+LWCZErVYPciMxuWL6zr1V0 21R4Q4IO/kX5yIJQrSDFN43haSqL4cV0pZ7ySq1WBL60vqeqbqrnE6Wep8dX/JQW+Xd2GV iTJ8OLoZngh9rIeIvmDtZBtZRm51uWo= X-MC-Unique: EF5HrFwLN8mQVnXpHYRH4A-1 From: Markus Armbruster To: qemu-devel@nongnu.org Subject: [PATCH for-5.1 1/8] tests-qemu-opts: Cover has_help_option(), qemu_opt_has_help_opt() Date: Thu, 9 Apr 2020 17:30:34 +0200 Message-Id: <20200409153041.17576-2-armbru@redhat.com> In-Reply-To: <20200409153041.17576-1-armbru@redhat.com> References: <20200409153041.17576-1-armbru@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.81 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, qemu-block@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" The two turn out to be inconsistent for "a,b,,help". Test case marked /* BUG */. Signed-off-by: Markus Armbruster --- tests/test-qemu-opts.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/test-qemu-opts.c b/tests/test-qemu-opts.c index ef96e84aed..0efe93b45e 100644 --- a/tests/test-qemu-opts.c +++ b/tests/test-qemu-opts.c @@ -728,6 +728,43 @@ static void test_opts_parse_size(void) qemu_opts_reset(&opts_list_02); } =20 +static void test_has_help_option(void) +{ + static const struct { + const char *params; + /* expected value of has_help_option() */ + bool expect_has_help_option; + /* expected value of qemu_opt_has_help_opt() with implied=3Dfalse = */ + bool expect_opt_has_help_opt; + /* expected value of qemu_opt_has_help_opt() with implied=3Dtrue */ + bool expect_opt_has_help_opt_implied; + } test[] =3D { + { "help", true, true, false }, + { "helpme", false, false, false }, + { "a,help", true, true, true }, + { "a=3D0,help,b", true, true, true }, + { "help,b=3D1", true, true, false }, + { "a,b,,help", false /* BUG */, true, true }, + }; + int i; + QemuOpts *opts; + + for (i =3D 0; i < ARRAY_SIZE(test); i++) { + g_assert_cmpint(has_help_option(test[i].params), + =3D=3D, test[i].expect_has_help_option); + opts =3D qemu_opts_parse(&opts_list_03, test[i].params, false, + &error_abort); + g_assert_cmpint(qemu_opt_has_help_opt(opts), + =3D=3D, test[i].expect_opt_has_help_opt); + qemu_opts_del(opts); + opts =3D qemu_opts_parse(&opts_list_03, test[i].params, true, + &error_abort); + g_assert_cmpint(qemu_opt_has_help_opt(opts), + =3D=3D, test[i].expect_opt_has_help_opt_implied); + qemu_opts_del(opts); + } +} + static void append_verify_list_01(QemuOptDesc *desc, bool with_overlapping) { int i =3D 0; @@ -990,6 +1027,7 @@ int main(int argc, char *argv[]) g_test_add_func("/qemu-opts/opts_parse/bool", test_opts_parse_bool); g_test_add_func("/qemu-opts/opts_parse/number", test_opts_parse_number= ); g_test_add_func("/qemu-opts/opts_parse/size", test_opts_parse_size); + g_test_add_func("/qemu-opts/has_help_option", test_has_help_option); g_test_add_func("/qemu-opts/append_to_null", test_opts_append_to_null); g_test_add_func("/qemu-opts/append", test_opts_append); g_test_add_func("/qemu-opts/to_qdict/basic", test_opts_to_qdict_basic); --=20 2.21.1 From nobody Sun May 19 03:54:32 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1586446485; cv=none; d=zohomail.com; s=zohoarc; b=AmJCRTw4aVKhkFaM0llH4qXJWf8YKr9+HxZGm2lfCH8bHGejMFRCYIZSajgxOxq+eso0qBb1iOMSyNaD3lNbIcx0e16Q0LHQlR1PLGvUY/lsybHeJg/33uJra7/WBJV1kX0AG9yEHqvYi01AONBHfl9CfHMzLmGGQ4JV5szkDyo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1586446485; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=N4StbKGvjqHRcrZFfc5OaMe1Xocyuir9bg9VtEJzmWo=; b=A7VlnnC4lBCl9EgMVw+692TeBvuWBSPj8iljgu6fNg5DYth+pZnSlpC8PnrQWLpmzRRfimGfT7GdLuV0wwp8D0sZvj20tHEXjI3uCY/4ZNCZLdIvwtFFDMsD8PVBVA01Fpki/SjTccJ2aiRGTYoQRoOkaTvNl0qyHVbYX3woxC0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1586446485472982.4653317263889; Thu, 9 Apr 2020 08:34:45 -0700 (PDT) Received: from localhost ([::1]:51802 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jMZCV-0006s0-UU for importer@patchew.org; Thu, 09 Apr 2020 11:34:43 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50515) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jMZ8j-00016X-Gn for qemu-devel@nongnu.org; Thu, 09 Apr 2020 11:30:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jMZ8i-0003eo-2F for qemu-devel@nongnu.org; Thu, 09 Apr 2020 11:30:49 -0400 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:26166 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jMZ8h-0003as-U6 for qemu-devel@nongnu.org; Thu, 09 Apr 2020 11:30:47 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-92-h4y8APE3NO-j_PAQfdL1Vg-1; Thu, 09 Apr 2020 11:30:43 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E7A56107ACC4; Thu, 9 Apr 2020 15:30:42 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-113-20.ams2.redhat.com [10.36.113.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B4F2A9D348; Thu, 9 Apr 2020 15:30:42 +0000 (UTC) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id 3A06611358AF; Thu, 9 Apr 2020 17:30:41 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1586446245; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=N4StbKGvjqHRcrZFfc5OaMe1Xocyuir9bg9VtEJzmWo=; b=Y3lIetz+gwRQ9hfBptPFI0JhPaB606UCMhGZugEM0l+30XUlPZd9ifoL+bb+cADge6Rdk+ MHVKFI4eEEITPzHTVoXBcyPoWQf7/gjmyHp9uvsvozOKsgttXRoupobQUZThf9PztTUfU9 EDKFJuuChz0Q3VXZY/A+b+/0ybCR+uA= X-MC-Unique: h4y8APE3NO-j_PAQfdL1Vg-1 From: Markus Armbruster To: qemu-devel@nongnu.org Subject: [PATCH for-5.1 2/8] qemu-options: Factor out get_opt_name_value() helper Date: Thu, 9 Apr 2020 17:30:35 +0200 Message-Id: <20200409153041.17576-3-armbru@redhat.com> In-Reply-To: <20200409153041.17576-1-armbru@redhat.com> References: <20200409153041.17576-1-armbru@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.61 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, qemu-block@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" The next commits will put it to use. Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake Reviewed-by: Kevin Wolf --- util/qemu-option.c | 102 +++++++++++++++++++++++++-------------------- 1 file changed, 56 insertions(+), 46 deletions(-) diff --git a/util/qemu-option.c b/util/qemu-option.c index 97172b5eaa..f08f4bc458 100644 --- a/util/qemu-option.c +++ b/util/qemu-option.c @@ -805,61 +805,71 @@ void qemu_opts_print(QemuOpts *opts, const char *sepa= rator) } } =20 +static const char *get_opt_name_value(const char *params, + const char *firstname, + char **name, char **value) +{ + const char *p, *pe, *pc; + + pe =3D strchr(params, '=3D'); + pc =3D strchr(params, ','); + + if (!pe || (pc && pc < pe)) { + /* found "foo,more" */ + if (firstname) { + /* implicitly named first option */ + *name =3D g_strdup(firstname); + p =3D get_opt_value(params, value); + } else { + /* option without value, must be a flag */ + p =3D get_opt_name(params, name, ','); + if (strncmp(*name, "no", 2) =3D=3D 0) { + memmove(*name, *name + 2, strlen(*name + 2) + 1); + *value =3D g_strdup("off"); + } else { + *value =3D g_strdup("on"); + } + } + } else { + /* found "foo=3Dbar,more" */ + p =3D get_opt_name(params, name, '=3D'); + assert(*p =3D=3D '=3D'); + p++; + p =3D get_opt_value(p, value); + } + + assert(!*p || *p =3D=3D ','); + if (*p =3D=3D ',') { + p++; + } + return p; +} + static void opts_do_parse(QemuOpts *opts, const char *params, const char *firstname, bool prepend, bool *invalidp, Error **errp) { - char *option =3D NULL; - char *value =3D NULL; - const char *p,*pe,*pc; Error *local_err =3D NULL; + char *option, *value; + const char *p; =20 - for (p =3D params; *p !=3D '\0'; p++) { - pe =3D strchr(p, '=3D'); - pc =3D strchr(p, ','); - if (!pe || (pc && pc < pe)) { - /* found "foo,more" */ - if (p =3D=3D params && firstname) { - /* implicitly named first option */ - option =3D g_strdup(firstname); - p =3D get_opt_value(p, &value); - } else { - /* option without value, probably a flag */ - p =3D get_opt_name(p, &option, ','); - if (strncmp(option, "no", 2) =3D=3D 0) { - memmove(option, option+2, strlen(option+2)+1); - value =3D g_strdup("off"); - } else { - value =3D g_strdup("on"); - } - } - } else { - /* found "foo=3Dbar,more" */ - p =3D get_opt_name(p, &option, '=3D'); - assert(*p =3D=3D '=3D'); - p++; - p =3D get_opt_value(p, &value); - } - if (strcmp(option, "id") !=3D 0) { - /* store and parse */ - opt_set(opts, option, value, prepend, invalidp, &local_err); - value =3D NULL; - if (local_err) { - error_propagate(errp, local_err); - goto cleanup; - } - } - if (*p !=3D ',') { - break; + for (p =3D params; *p;) { + p =3D get_opt_name_value(p, firstname, &option, &value); + firstname =3D NULL; + + if (!strcmp(option, "id")) { + g_free(option); + g_free(value); + continue; } + + opt_set(opts, option, value, prepend, invalidp, &local_err); g_free(option); - g_free(value); - option =3D value =3D NULL; + if (local_err) { + error_propagate(errp, local_err); + return; + } } - - cleanup: - g_free(option); - g_free(value); } =20 /** --=20 2.21.1 From nobody Sun May 19 03:54:32 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1586446612; cv=none; d=zohomail.com; s=zohoarc; b=XS6zKS0JHS5/x8Afs/DH+8U63NADIiMBGbv5sGeKz00RmuOpo2f9QWWv8D7rDurrYEBaPBCr2hMm63pysqURsNOdP0PStFtvuatcSIkVWwkeJnvRTQCUJNW6JGuNlT8iSsZhq6kmRpeZ7WicYeyszIjjbFo1lqNHZfc5cfERQFc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1586446612; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=KIXMIY3z990fornc63vrvyKXThkU3RpBJQy/R3PtVCE=; b=aYOar5VSCwFf5NIXh6CN6fZbOhFlE9Ak63kNkPDYhntmMe2aULN1X3/JJSuvyTC3mx1T/Kra7PtsoJ3jdR2pxYz0GI5O1UEGY54j2me+XuiSOWuRggxQWW1m1EWf9wvI12PNXtO4Z3hgUcJ0JrXf9RVGWxRKATryJNcplZDmGLE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1586446612980747.9355495157845; Thu, 9 Apr 2020 08:36:52 -0700 (PDT) Received: from localhost ([::1]:51846 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jMZEZ-0001de-O1 for importer@patchew.org; Thu, 09 Apr 2020 11:36:51 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50522) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jMZ8j-00016f-Ki for qemu-devel@nongnu.org; Thu, 09 Apr 2020 11:30:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jMZ8i-0003fH-99 for qemu-devel@nongnu.org; Thu, 09 Apr 2020 11:30:49 -0400 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:30528 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jMZ8i-0003dy-4M for qemu-devel@nongnu.org; Thu, 09 Apr 2020 11:30:48 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-92-SsEXkKC4Okab5Vjbg20mFg-1; Thu, 09 Apr 2020 11:30:43 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id E8C10107ACCC; Thu, 9 Apr 2020 15:30:42 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-113-20.ams2.redhat.com [10.36.113.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B54D011959E; Thu, 9 Apr 2020 15:30:42 +0000 (UTC) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id 3D2CF11358B0; Thu, 9 Apr 2020 17:30:41 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1586446247; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=KIXMIY3z990fornc63vrvyKXThkU3RpBJQy/R3PtVCE=; b=Wd0owz8oqRth64KMGgEWR7wpf5I72oBEmIwl0docaVHamJsgvwOWgDrZkbIqEumwQx1ctf iSVYCmX8qD0d8CashMQoPkaR1YUpGwcVThCxCMKaSocUiskEGpISFEZfYGcp08yliI+TjX Lu8kyOqiEqJC2QqEcfPf5eM80CimgGw= X-MC-Unique: SsEXkKC4Okab5Vjbg20mFg-1 From: Markus Armbruster To: qemu-devel@nongnu.org Subject: [PATCH for-5.1 3/8] qemu-option: Fix sloppy recognition of "id=..." after ", , " Date: Thu, 9 Apr 2020 17:30:36 +0200 Message-Id: <20200409153041.17576-4-armbru@redhat.com> In-Reply-To: <20200409153041.17576-1-armbru@redhat.com> References: <20200409153041.17576-1-armbru@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, qemu-block@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake Reviewed-by: Kevin Wolf --- tests/test-qemu-opts.c | 4 ++-- util/qemu-option.c | 27 +++++++++++++++++++-------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/tests/test-qemu-opts.c b/tests/test-qemu-opts.c index 0efe93b45e..27c24bb1a2 100644 --- a/tests/test-qemu-opts.c +++ b/tests/test-qemu-opts.c @@ -500,10 +500,10 @@ static void test_opts_parse(void) g_assert(!opts); /* TODO Cover .merge_lists =3D true */ =20 - /* Buggy ID recognition */ + /* Buggy ID recognition (fixed) */ opts =3D qemu_opts_parse(&opts_list_03, "x=3D,,id=3Dbar", false, &erro= r_abort); g_assert_cmpuint(opts_count(opts), =3D=3D, 1); - g_assert_cmpstr(qemu_opts_id(opts), =3D=3D, "bar"); /* BUG */ + g_assert(!qemu_opts_id(opts)); g_assert_cmpstr(qemu_opt_get(opts, "x"), =3D=3D, ",id=3Dbar"); =20 /* Anti-social ID */ diff --git a/util/qemu-option.c b/util/qemu-option.c index f08f4bc458..d2956082bd 100644 --- a/util/qemu-option.c +++ b/util/qemu-option.c @@ -872,6 +872,24 @@ static void opts_do_parse(QemuOpts *opts, const char *= params, } } =20 +static char *opts_parse_id(const char *params) +{ + const char *p; + char *name, *value; + + for (p =3D params; *p;) { + p =3D get_opt_name_value(p, NULL, &name, &value); + if (!strcmp(name, "id")) { + g_free(name); + return value; + } + g_free(name); + g_free(value); + } + + return NULL; +} + /** * Store options parsed from @params into @opts. * If @firstname is non-null, the first key=3Dvalue in @params may omit @@ -889,20 +907,13 @@ static QemuOpts *opts_parse(QemuOptsList *list, const= char *params, bool *invalidp, Error **errp) { const char *firstname; - char *id =3D NULL; - const char *p; + char *id =3D opts_parse_id(params); QemuOpts *opts; Error *local_err =3D NULL; =20 assert(!permit_abbrev || list->implied_opt_name); firstname =3D permit_abbrev ? list->implied_opt_name : NULL; =20 - if (strncmp(params, "id=3D", 3) =3D=3D 0) { - get_opt_value(params + 3, &id); - } else if ((p =3D strstr(params, ",id=3D")) !=3D NULL) { - get_opt_value(p + 4, &id); - } - /* * This code doesn't work for defaults && !list->merge_lists: when * params has no id=3D, and list has an element with !opts->id, it --=20 2.21.1 From nobody Sun May 19 03:54:32 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1586446657; cv=none; d=zohomail.com; s=zohoarc; b=Dnuo40q/6+0zhFLeu0BYSdMwf4bzV3Jx7HxaLq0o08s9I+dKif5Bi1a3y65yQMEsSyjZI6zccIWXHGmD9r+f4FHPOfcSJBo7YX7IhH1ymLi+mJIVbB2fHy/SjJ2Zn8kA5ANeY7Zr7Oi9oYUxQxTYJOOf7DtKjQuSlwyCILkoQcw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1586446657; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=fcSuBaAbHjMGVuJ6BNqgEJ7D9xgolLjUFkT2uLU/lQE=; b=d8wD7KCHzaucRn3WVB/U3SPB4oovVDeKiShHbgp8iRNLHLv1Z8xZcxl4wuaHFASdZvvEkOt6yzy59NaI2np4k3oMgJukT11gygHFPDvWegLJ2PNbjSCm3VSLOlfdwaU5S1qoRLFhnghui+gJpVnAVso+kR9x26ZLGgqP+MvQWmM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1586446657936790.634122578909; Thu, 9 Apr 2020 08:37:37 -0700 (PDT) Received: from localhost ([::1]:51858 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jMZFI-00030s-IJ for importer@patchew.org; Thu, 09 Apr 2020 11:37:36 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50546) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jMZ8k-000170-34 for qemu-devel@nongnu.org; Thu, 09 Apr 2020 11:30:51 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jMZ8i-0003fX-IP for qemu-devel@nongnu.org; Thu, 09 Apr 2020 11:30:50 -0400 Received: from us-smtp-1.mimecast.com ([207.211.31.81]:28881 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jMZ8i-0003ev-DV for qemu-devel@nongnu.org; Thu, 09 Apr 2020 11:30:48 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-247-u6545Eh2PkWG10piBksIqA-1; Thu, 09 Apr 2020 11:30:44 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id EFA7D800D5B; Thu, 9 Apr 2020 15:30:42 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-113-20.ams2.redhat.com [10.36.113.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id BB9381195A4; Thu, 9 Apr 2020 15:30:42 +0000 (UTC) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id 4068311358B1; Thu, 9 Apr 2020 17:30:41 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1586446248; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=fcSuBaAbHjMGVuJ6BNqgEJ7D9xgolLjUFkT2uLU/lQE=; b=f2E6bZcqmn4BgBmfphWfba5bWxjv+ZUVK/qaxiEAsWkyekZElQw3H7NGwCf6CDyYVoFf89 s6pIKaldV6T6GxEtjcErU/2C6yxrN1gyxK/Wg0Gsgs42chIrDC58gS6Is+NvSee0FnCWdr /diPx/lPAx54J9/MX7sXOjmr+Xjdc4c= X-MC-Unique: u6545Eh2PkWG10piBksIqA-1 From: Markus Armbruster To: qemu-devel@nongnu.org Subject: [PATCH for-5.1 4/8] qemu-option: Avoid has_help_option() in qemu_opts_parse_noisily() Date: Thu, 9 Apr 2020 17:30:37 +0200 Message-Id: <20200409153041.17576-5-armbru@redhat.com> In-Reply-To: <20200409153041.17576-1-armbru@redhat.com> References: <20200409153041.17576-1-armbru@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.81 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, qemu-block@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" When opts_parse() sets @invalidp to true, qemu_opts_parse_noisily() uses has_help_option() to decide whether to print help. This parses the input string a second time, in a slightly different way. Easy to avoid: replace @invalidp by @help_wanted. Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake --- util/qemu-option.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/util/qemu-option.c b/util/qemu-option.c index d2956082bd..6403e521fc 100644 --- a/util/qemu-option.c +++ b/util/qemu-option.c @@ -539,7 +539,7 @@ int qemu_opt_unset(QemuOpts *opts, const char *name) } =20 static void opt_set(QemuOpts *opts, const char *name, char *value, - bool prepend, bool *invalidp, Error **errp) + bool prepend, bool *help_wanted, Error **errp) { QemuOpt *opt; const QemuOptDesc *desc; @@ -549,8 +549,8 @@ static void opt_set(QemuOpts *opts, const char *name, c= har *value, if (!desc && !opts_accepts_any(opts)) { g_free(value); error_setg(errp, QERR_INVALID_PARAMETER, name); - if (invalidp) { - *invalidp =3D true; + if (help_wanted && is_help_option(name)) { + *help_wanted =3D true; } return; } @@ -847,7 +847,7 @@ static const char *get_opt_name_value(const char *param= s, =20 static void opts_do_parse(QemuOpts *opts, const char *params, const char *firstname, bool prepend, - bool *invalidp, Error **errp) + bool *help_wanted, Error **errp) { Error *local_err =3D NULL; char *option, *value; @@ -863,7 +863,7 @@ static void opts_do_parse(QemuOpts *opts, const char *p= arams, continue; } =20 - opt_set(opts, option, value, prepend, invalidp, &local_err); + opt_set(opts, option, value, prepend, help_wanted, &local_err); g_free(option); if (local_err) { error_propagate(errp, local_err); @@ -904,7 +904,7 @@ void qemu_opts_do_parse(QemuOpts *opts, const char *par= ams, =20 static QemuOpts *opts_parse(QemuOptsList *list, const char *params, bool permit_abbrev, bool defaults, - bool *invalidp, Error **errp) + bool *help_wanted, Error **errp) { const char *firstname; char *id =3D opts_parse_id(params); @@ -929,7 +929,7 @@ static QemuOpts *opts_parse(QemuOptsList *list, const c= har *params, return NULL; } =20 - opts_do_parse(opts, params, firstname, defaults, invalidp, &local_err); + opts_do_parse(opts, params, firstname, defaults, help_wanted, &local_e= rr); if (local_err) { error_propagate(errp, local_err); qemu_opts_del(opts); @@ -965,11 +965,11 @@ QemuOpts *qemu_opts_parse_noisily(QemuOptsList *list,= const char *params, { Error *err =3D NULL; QemuOpts *opts; - bool invalidp =3D false; + bool help_wanted =3D false; =20 - opts =3D opts_parse(list, params, permit_abbrev, false, &invalidp, &er= r); + opts =3D opts_parse(list, params, permit_abbrev, false, &help_wanted, = &err); if (err) { - if (invalidp && has_help_option(params)) { + if (help_wanted) { qemu_opts_print_help(list, true); error_free(err); } else { --=20 2.21.1 From nobody Sun May 19 03:54:32 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1586446336; cv=none; d=zohomail.com; s=zohoarc; b=LV3pZb3eTLqMNela927qqrnywk42+Cm2feFzEcp/C0bc3oLzhRV+3iTyRD4i1ypaBcSFSfuAuH7I3CwMGQo+heoqlI+qnJFlF8SM6iVuvB5FJf7rtOmjYCfWmNqIBbabENv7HKyIFeJjuSsWvg/3xT38/UmVCvv8rAe0gw5PrTI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1586446336; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=gOCUd8UN3wzT8fEQwhe54oUKmFBLgPdAHxu8KZRWRng=; b=NQ2ye7WfVAwMMDXpD/EeUVw069fVkoY6AnCI/H3KoRLJ+Z54sylJZed5ULPq5tNLQ7bgALLAUh0iJzy/TM0sQ4l4kQqAWSiopTccNHodaL0vtyrQvzcEm/m6KE8To8LjlVEfDN696Rm4kDeNO9BLFldM93JVnuicQGeb9OIDm7g= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1586446336896271.226718175408; Thu, 9 Apr 2020 08:32:16 -0700 (PDT) Received: from localhost ([::1]:51756 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jMZA7-0002tE-HS for importer@patchew.org; Thu, 09 Apr 2020 11:32:15 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50602) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jMZ8l-00019P-Hn for qemu-devel@nongnu.org; Thu, 09 Apr 2020 11:30:53 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jMZ8k-0003iZ-D6 for qemu-devel@nongnu.org; Thu, 09 Apr 2020 11:30:51 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:48036 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jMZ8k-0003hs-A5 for qemu-devel@nongnu.org; Thu, 09 Apr 2020 11:30:50 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-363-0F6hhmfNNkqBsbSD6xP5lQ-1; Thu, 09 Apr 2020 11:30:45 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 694F818FF661; Thu, 9 Apr 2020 15:30:44 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-113-20.ams2.redhat.com [10.36.113.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 37BCA9D348; Thu, 9 Apr 2020 15:30:44 +0000 (UTC) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id 438CA11358B2; Thu, 9 Apr 2020 17:30:41 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1586446250; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gOCUd8UN3wzT8fEQwhe54oUKmFBLgPdAHxu8KZRWRng=; b=XqqTAJcO90/SnYHrstpLvSgEtVopFBqsWJDUiZvparBCGAK1qn1XT5oxBp9ui521ExOfqr PZyY5T17FpknLY6OJ5vYrLA7+HSddS4qamtGqcaaMamoKrCEX68uaB6oI08WnijB8sLWwy kP0EVWLckWCtc0freoR8vOK44bAdhTk= X-MC-Unique: 0F6hhmfNNkqBsbSD6xP5lQ-1 From: Markus Armbruster To: qemu-devel@nongnu.org Subject: [PATCH for-5.1 5/8] qemu-option: Fix has_help_option()'s sloppy parsing Date: Thu, 9 Apr 2020 17:30:38 +0200 Message-Id: <20200409153041.17576-6-armbru@redhat.com> In-Reply-To: <20200409153041.17576-1-armbru@redhat.com> References: <20200409153041.17576-1-armbru@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, qemu-block@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" has_help_option() uses its own parser. It's inconsistent with qemu_opts_parse(), as demonstrated by test-qemu-opts case /qemu-opts/has_help_option. Fix by reusing the common parser. Signed-off-by: Markus Armbruster --- tests/test-qemu-opts.c | 2 +- util/qemu-option.c | 39 +++++++++++++++++++-------------------- 2 files changed, 20 insertions(+), 21 deletions(-) diff --git a/tests/test-qemu-opts.c b/tests/test-qemu-opts.c index 27c24bb1a2..58a4ea2408 100644 --- a/tests/test-qemu-opts.c +++ b/tests/test-qemu-opts.c @@ -744,7 +744,7 @@ static void test_has_help_option(void) { "a,help", true, true, true }, { "a=3D0,help,b", true, true, true }, { "help,b=3D1", true, true, false }, - { "a,b,,help", false /* BUG */, true, true }, + { "a,b,,help", true, true, true }, }; int i; QemuOpts *opts; diff --git a/util/qemu-option.c b/util/qemu-option.c index 6403e521fc..279f1b3fb3 100644 --- a/util/qemu-option.c +++ b/util/qemu-option.c @@ -165,26 +165,6 @@ void parse_option_size(const char *name, const char *v= alue, *ret =3D size; } =20 -bool has_help_option(const char *param) -{ - const char *p =3D param; - bool result =3D false; - - while (*p && !result) { - char *value; - - p =3D get_opt_value(p, &value); - if (*p) { - p++; - } - - result =3D is_help_option(value); - g_free(value); - } - - return result; -} - bool is_valid_option_list(const char *p) { char *value =3D NULL; @@ -890,6 +870,25 @@ static char *opts_parse_id(const char *params) return NULL; } =20 +bool has_help_option(const char *params) +{ + const char *p; + char *name, *value; + bool ret; + + for (p =3D params; *p;) { + p =3D get_opt_name_value(p, NULL, &name, &value); + ret =3D !strcmp(name, "help"); + g_free(name); + g_free(value); + if (ret) { + return true; + } + } + + return false; +} + /** * Store options parsed from @params into @opts. * If @firstname is non-null, the first key=3Dvalue in @params may omit --=20 2.21.1 From nobody Sun May 19 03:54:32 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1586446343; cv=none; d=zohomail.com; s=zohoarc; b=fTXouYZEeYj1xj1LZkbg8ZjvFzYCdALPCFzbvgF1lMxHLydOD8QzE+iFQ2Hx896fxvoAJPSvWDjLv8EMeTT9hrf7UW2nw7qTY87j5fWByP5wzQfeQwFjlUDqB/pSWWYC1OZRNly2eNp4efNdIDpEUW8OKM0Cue0/6K7iULIOLx0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1586446343; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=2+q0zPLFj+Kzgjxh0ev/nr4AhppV/klWNtrPOLt/A9o=; b=bV6cSYuQAkYePMyndt7ZsTpOSGNez1SDVR1frdxpB/MPBqWhILnDa2ZV87NahkBRnqGfGYnXsZHTSsoiPdMRZKtcZo1+CFxukpkGjPEQSOjeRQEGlT/jEATJQDS8tv+UdBF0JCK7+5jmN2IBGwh2CSAr8RHAU1/R53xKGq7dyTg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1586446343407887.1886714731787; Thu, 9 Apr 2020 08:32:23 -0700 (PDT) Received: from localhost ([::1]:51758 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jMZAD-00039c-W4 for importer@patchew.org; Thu, 09 Apr 2020 11:32:22 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50521) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jMZ8j-00016e-Ki for qemu-devel@nongnu.org; Thu, 09 Apr 2020 11:30:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jMZ8h-0003eC-Sd for qemu-devel@nongnu.org; Thu, 09 Apr 2020 11:30:49 -0400 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:48154 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jMZ8h-0003d9-OA for qemu-devel@nongnu.org; Thu, 09 Apr 2020 11:30:47 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-514-O4wSgMWVMx-mnvmQidsx-Q-1; Thu, 09 Apr 2020 11:30:45 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6CA22800D5C; Thu, 9 Apr 2020 15:30:44 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-113-20.ams2.redhat.com [10.36.113.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3884C11959E; Thu, 9 Apr 2020 15:30:44 +0000 (UTC) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id 46AF411358B3; Thu, 9 Apr 2020 17:30:41 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1586446247; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=2+q0zPLFj+Kzgjxh0ev/nr4AhppV/klWNtrPOLt/A9o=; b=bSDoZSVEJB3IHBHZCd7D2rWeu4+3l9tFRPK+1oS8wkaJ3omLS4eLOA5F0+qAeIAbN3GHDa QY+7q4neN/8RmhGBP3JxHCGYLxLLdBe0RGpRTugN36vNNCoqFm9NXSsKrLt/DD6nQvMLbW 5/zNeq6NS772+Wviw0F5nfCP3SURU1M= X-MC-Unique: O4wSgMWVMx-mnvmQidsx-Q-1 From: Markus Armbruster To: qemu-devel@nongnu.org Subject: [PATCH for-5.1 6/8] test-qemu-opts: Simplify test_has_help_option() after bug fix Date: Thu, 9 Apr 2020 17:30:39 +0200 Message-Id: <20200409153041.17576-7-armbru@redhat.com> In-Reply-To: <20200409153041.17576-1-armbru@redhat.com> References: <20200409153041.17576-1-armbru@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.61 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, qemu-block@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake Reviewed-by: Kevin Wolf --- tests/test-qemu-opts.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/tests/test-qemu-opts.c b/tests/test-qemu-opts.c index 58a4ea2408..909118d617 100644 --- a/tests/test-qemu-opts.c +++ b/tests/test-qemu-opts.c @@ -732,35 +732,33 @@ static void test_has_help_option(void) { static const struct { const char *params; - /* expected value of has_help_option() */ - bool expect_has_help_option; /* expected value of qemu_opt_has_help_opt() with implied=3Dfalse = */ - bool expect_opt_has_help_opt; + bool expect; /* expected value of qemu_opt_has_help_opt() with implied=3Dtrue */ - bool expect_opt_has_help_opt_implied; + bool expect_implied; } test[] =3D { - { "help", true, true, false }, - { "helpme", false, false, false }, - { "a,help", true, true, true }, - { "a=3D0,help,b", true, true, true }, - { "help,b=3D1", true, true, false }, - { "a,b,,help", true, true, true }, + { "help", true, false }, + { "helpme", false, false }, + { "a,help", true, true }, + { "a=3D0,help,b", true, true }, + { "help,b=3D1", true, false }, + { "a,b,,help", true, true }, }; int i; QemuOpts *opts; =20 for (i =3D 0; i < ARRAY_SIZE(test); i++) { g_assert_cmpint(has_help_option(test[i].params), - =3D=3D, test[i].expect_has_help_option); + =3D=3D, test[i].expect); opts =3D qemu_opts_parse(&opts_list_03, test[i].params, false, &error_abort); g_assert_cmpint(qemu_opt_has_help_opt(opts), - =3D=3D, test[i].expect_opt_has_help_opt); + =3D=3D, test[i].expect); qemu_opts_del(opts); opts =3D qemu_opts_parse(&opts_list_03, test[i].params, true, &error_abort); g_assert_cmpint(qemu_opt_has_help_opt(opts), - =3D=3D, test[i].expect_opt_has_help_opt_implied); + =3D=3D, test[i].expect_implied); qemu_opts_del(opts); } } --=20 2.21.1 From nobody Sun May 19 03:54:32 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1586446602; cv=none; d=zohomail.com; s=zohoarc; b=MbocytWdeoITWozQa2JyQOF0PApXgzFcgeP37S3H3fk5MWnmXE9jZtus9vw0pJF9d/TtoLHOXGpBZW2LlPtU/2hJmiV9DnnkZo2+3muecWegfKIvvIu3EZIuRpPeoeJuCo2hMNhsSW81Eoc7uf0t4n/yZIekK+wIfG6KKwDJWAc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1586446602; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=D52HEWAzlQgS5JHdsYMyhl6MkozwaRB13MAOJ0BhICM=; b=AgHmrqNyC6meJDHoenIu8QoESFfsIk/puHS/e40RLEbP7qcmE8OePvp5zahxOyonElBknFzk/8U6BIjDhzl66hbR54CW5TWM4RIBkYoi78KugY5JJRYlAFxGjtGJLBtPMMntbsytutjVdOIvRObyJZMTXw3ChwDVK5qtqc7qQGU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1586446602301416.97432820902145; Thu, 9 Apr 2020 08:36:42 -0700 (PDT) Received: from localhost ([::1]:51842 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jMZEP-0001EL-2c for importer@patchew.org; Thu, 09 Apr 2020 11:36:41 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50477) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jMZ8i-00016P-Rr for qemu-devel@nongnu.org; Thu, 09 Apr 2020 11:30:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jMZ8h-0003dJ-IS for qemu-devel@nongnu.org; Thu, 09 Apr 2020 11:30:48 -0400 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:35250 helo=us-smtp-delivery-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jMZ8h-0003ce-ES for qemu-devel@nongnu.org; Thu, 09 Apr 2020 11:30:47 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-246-47Mofi4lOB-d3n35iuaECg-1; Thu, 09 Apr 2020 11:30:45 -0400 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6AC0F8017F3; Thu, 9 Apr 2020 15:30:44 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-113-20.ams2.redhat.com [10.36.113.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 378AD5C545; Thu, 9 Apr 2020 15:30:44 +0000 (UTC) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id 4A91E11358B7; Thu, 9 Apr 2020 17:30:41 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1586446247; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=D52HEWAzlQgS5JHdsYMyhl6MkozwaRB13MAOJ0BhICM=; b=FWD2mG2yDOltLUSxQ/nE/qYDXuxcQ1zoDSfidUFg5ARG00PYJU5yPTbSBYFmVg4J1RWMwg VXZsx1NYOz/cPGKWwxf18Q8tNx2r4wwTFGFjAje/IjbjX5c6yL87IDENh9Pe/pvqxSL3Ok t+yo3yl0ChL94pZWSG0UwpipCWE/W8w= X-MC-Unique: 47Mofi4lOB-d3n35iuaECg-1 From: Markus Armbruster To: qemu-devel@nongnu.org Subject: [PATCH for-5.1 7/8] qemu-img: Factor out accumulate_options() helper Date: Thu, 9 Apr 2020 17:30:40 +0200 Message-Id: <20200409153041.17576-8-armbru@redhat.com> In-Reply-To: <20200409153041.17576-1-armbru@redhat.com> References: <20200409153041.17576-1-armbru@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 205.139.110.61 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, qemu-block@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake Reviewed-by: Kevin Wolf --- qemu-img.c | 59 +++++++++++++++++++++--------------------------------- 1 file changed, 23 insertions(+), 36 deletions(-) diff --git a/qemu-img.c b/qemu-img.c index b167376bd7..28b27b738a 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -223,6 +223,25 @@ static bool qemu_img_object_print_help(const char *typ= e, QemuOpts *opts) return true; } =20 +static int accumulate_options(char **options, char *optarg) +{ + char *new_options; + + if (!is_valid_option_list(optarg)) { + error_report("Invalid option list: %s", optarg); + return -1; + } + + if (!*options) { + *options =3D g_strdup(optarg); + } else { + new_options =3D g_strdup_printf("%s,%s", *options, optarg); + g_free(*options); + *options =3D new_options; + } + return 0; +} + static QemuOptsList qemu_source_opts =3D { .name =3D "source", .implied_opt_name =3D "file", @@ -482,17 +501,9 @@ static int img_create(int argc, char **argv) fmt =3D optarg; break; case 'o': - if (!is_valid_option_list(optarg)) { - error_report("Invalid option list: %s", optarg); + if (accumulate_options(&options, optarg) < 0) { goto fail; } - if (!options) { - options =3D g_strdup(optarg); - } else { - char *old_options =3D options; - options =3D g_strdup_printf("%s,%s", options, optarg); - g_free(old_options); - } break; case 'q': quiet =3D true; @@ -2127,17 +2138,9 @@ static int img_convert(int argc, char **argv) s.compressed =3D true; break; case 'o': - if (!is_valid_option_list(optarg)) { - error_report("Invalid option list: %s", optarg); + if (accumulate_options(&options, optarg) < 0) { goto fail_getopt; } - if (!options) { - options =3D g_strdup(optarg); - } else { - char *old_options =3D options; - options =3D g_strdup_printf("%s,%s", options, optarg); - g_free(old_options); - } break; case 'l': if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { @@ -3953,18 +3956,10 @@ static int img_amend(int argc, char **argv) help(); break; case 'o': - if (!is_valid_option_list(optarg)) { - error_report("Invalid option list: %s", optarg); + if (accumulate_options(&options, optarg) < 0) { ret =3D -1; goto out_no_progress; } - if (!options) { - options =3D g_strdup(optarg); - } else { - char *old_options =3D options; - options =3D g_strdup_printf("%s,%s", options, optarg); - g_free(old_options); - } break; case 'f': fmt =3D optarg; @@ -4855,17 +4850,9 @@ static int img_measure(int argc, char **argv) out_fmt =3D optarg; break; case 'o': - if (!is_valid_option_list(optarg)) { - error_report("Invalid option list: %s", optarg); + if (accumulate_options(&options, optarg) < 0) { goto out; } - if (!options) { - options =3D g_strdup(optarg); - } else { - char *old_options =3D options; - options =3D g_strdup_printf("%s,%s", options, optarg); - g_free(old_options); - } break; case 'l': if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { --=20 2.21.1 From nobody Sun May 19 03:54:32 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1586446481; cv=none; d=zohomail.com; s=zohoarc; b=Vo9d62AKzYSzXWfvMou2pbkeJBDk9b505xXnBDTr1jLs+Oh2viJARm8se8hUVprJQOOKWW9y3vtsa5GUmpo2WghuQ28M8iK8//L04BBQbAtSzLprxqYEYR1vbull0i/3z06U0F7+Dzvclus/WGd5b36DJIeUc2IovGNpVuiTUns= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1586446481; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=mB2y7knBNKmOhcBsBrN+C8nK1DyT+qjybYsMC2tHh7M=; b=AcyL+BLdlk7TwVievuQbDQfKD+tSGlL1DyUq6br0G0VivaHAeiUnKGgLRVyI7eU+R0L86mI1Ry/f8mBUajl0dQ/whFc/JKXnyElDb/i5W9Z7LQI0e2uwlEZoD98V9d+6gpJPy+bGYw/D9rN3IJbGrmiG4087Oal+9s/xmfQ0azI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1586446481896850.3741962230488; Thu, 9 Apr 2020 08:34:41 -0700 (PDT) Received: from localhost ([::1]:51800 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jMZCS-0006r9-LJ for importer@patchew.org; Thu, 09 Apr 2020 11:34:40 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:50506) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jMZ8j-00016U-75 for qemu-devel@nongnu.org; Thu, 09 Apr 2020 11:30:50 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1jMZ8h-0003dw-Pv for qemu-devel@nongnu.org; Thu, 09 Apr 2020 11:30:49 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:52451 helo=us-smtp-1.mimecast.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1jMZ8h-0003cv-Kr for qemu-devel@nongnu.org; Thu, 09 Apr 2020 11:30:47 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-73-ZmeEOw5SPa6HKsJ-oCHW9w-1; Thu, 09 Apr 2020 11:30:45 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6D2938018A1; Thu, 9 Apr 2020 15:30:44 +0000 (UTC) Received: from blackfin.pond.sub.org (ovpn-113-20.ams2.redhat.com [10.36.113.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 38ACA60BF3; Thu, 9 Apr 2020 15:30:44 +0000 (UTC) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id 4DC2F11358BB; Thu, 9 Apr 2020 17:30:41 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1586446247; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mB2y7knBNKmOhcBsBrN+C8nK1DyT+qjybYsMC2tHh7M=; b=hHbD/Z9O1AJRdjczCKhhWu8vdw2bvZKX+TmWzj9NSbZ2lqv+SzI2cXKbPGGNBTBGfLfYVp JG8l/9fPTlubcn3ua5FrlGkg5R6qBiEbz2cW/cUtAUQDy62ZGndz/ueUhqxv9s20yAL2kS ZGKTCsVv3pZF1bkVwUHrqAFRZMrfbK8= X-MC-Unique: ZmeEOw5SPa6HKsJ-oCHW9w-1 From: Markus Armbruster To: qemu-devel@nongnu.org Subject: [PATCH for-5.1 8/8] qemu-option: Move is_valid_option_list() to qemu-img.c and rewrite Date: Thu, 9 Apr 2020 17:30:41 +0200 Message-Id: <20200409153041.17576-9-armbru@redhat.com> In-Reply-To: <20200409153041.17576-1-armbru@redhat.com> References: <20200409153041.17576-1-armbru@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 207.211.31.120 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, qemu-block@nongnu.org, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" is_valid_option_list()'s purpose is ensuring qemu-img.c's can safely join multiple parameter strings separated by ',' like this: g_strdup_printf("%s,%s", params1, params2); How it does that is anything but obvious. A close reading of the code reveals that it fails exactly when its argument starts with ',' or ends with an odd number of ','. Makes sense, actually, because when the argument starts with ',', a separating ',' preceding it would get escaped, and when it ends with an odd number of ',', a separating ',' following it would get escaped. Move it to qemu-img.c and rewrite it the obvious way. Signed-off-by: Markus Armbruster Reviewed-by: Eric Blake --- include/qemu/option.h | 1 - qemu-img.c | 26 ++++++++++++++++++++++++++ util/qemu-option.c | 22 ---------------------- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/include/qemu/option.h b/include/qemu/option.h index 844587cab3..eb4097889d 100644 --- a/include/qemu/option.h +++ b/include/qemu/option.h @@ -33,7 +33,6 @@ const char *get_opt_value(const char *p, char **value); void parse_option_size(const char *name, const char *value, uint64_t *ret, Error **errp); bool has_help_option(const char *param); -bool is_valid_option_list(const char *param); =20 enum QemuOptType { QEMU_OPT_STRING =3D 0, /* no parsing (use string as-is) = */ diff --git a/qemu-img.c b/qemu-img.c index 28b27b738a..d8ca8a73e2 100644 --- a/qemu-img.c +++ b/qemu-img.c @@ -223,6 +223,32 @@ static bool qemu_img_object_print_help(const char *typ= e, QemuOpts *opts) return true; } =20 +/* + * Is @optarg safe for accumulate_options()? + * It is when multiple of them can be joined together separated by ','. + * To make that work, @optarg must not start with ',' (or else a + * separating ',' preceding it gets escaped), and it must not end with + * an odd number of ',' (or else a separating ',' following it gets + * escaped). + */ +static bool is_valid_option_list(const char *optarg) +{ + size_t len =3D strlen(optarg); + size_t i; + + if (optarg[0] =3D=3D ',') { + return false; + } + + for (i =3D len; i > 0 && optarg[i - 1] =3D=3D ','; i--) { + } + if ((len - i) % 2) { + return false; + } + + return true; +} + static int accumulate_options(char **options, char *optarg) { char *new_options; diff --git a/util/qemu-option.c b/util/qemu-option.c index 279f1b3fb3..6f80807738 100644 --- a/util/qemu-option.c +++ b/util/qemu-option.c @@ -165,28 +165,6 @@ void parse_option_size(const char *name, const char *v= alue, *ret =3D size; } =20 -bool is_valid_option_list(const char *p) -{ - char *value =3D NULL; - bool result =3D false; - - while (*p) { - p =3D get_opt_value(p, &value); - if ((*p && !*++p) || - (!*value || *value =3D=3D ',')) { - goto out; - } - - g_free(value); - value =3D NULL; - } - - result =3D true; -out: - g_free(value); - return result; -} - static const char *opt_type_to_string(enum QemuOptType type) { switch (type) { --=20 2.21.1