From nobody Sat Feb 7 05:49:43 2026 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