From nobody Thu Apr 3 10:05:08 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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 ARC-Seal: i=1; a=rsa-sha256; t=1741241875; cv=none; d=zohomail.com; s=zohoarc; b=bEOHlt9RKskxxxLTRnFCWpo8ytKXmKwlGWXWb8Ru23ybBwlzaO2TMPX4/KEKvuwtJLCXAVZlcRQJUCWAu0qpkmOrIbTxMkonm7A3QbMimYgtrGQkNAbQzwS8ecQISUMLaJbUepQkJ2JqRRstH3N8GJrX6aIVGvMKwAJM6+FIgb8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1741241875; h=Content-Type:Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=PNVlTce1uhHhS6pAL94t07YGCV3qzSeEltruk5DehEo=; b=CWsrqdJ3mGDc71eNvSY+xNfHqhf1pmAgUYCk9TBv8S3L9e1gCoS1+e+ol6wpogAr0Gkxh1lEsHlhQYK0x1jgdFoVPx6TArsIAfoiP9KpG/TrfJqbz3A7K+4yAg9FKtEy6a9MMgK/LYkCGqx9cSvgRu8RwGwmjPLnV7UBTNR8n4o= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1741241875016363.3103230119399; Wed, 5 Mar 2025 22:17:55 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tq4XJ-00029L-16; Thu, 06 Mar 2025 01:16:49 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tq4XH-000292-IA for qemu-devel@nongnu.org; Thu, 06 Mar 2025 01:16:47 -0500 Received: from mail-pl1-x630.google.com ([2607:f8b0:4864:20::630]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tq4XF-0003VD-8R for qemu-devel@nongnu.org; Thu, 06 Mar 2025 01:16:47 -0500 Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-22382657540so3880355ad.2 for ; Wed, 05 Mar 2025 22:16:44 -0800 (PST) Received: from localhost ([157.82.207.107]) by smtp.gmail.com with UTF8SMTPSA id d9443c01a7336-22410a918e5sm4244205ad.167.2025.03.05.22.16.39 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 05 Mar 2025 22:16:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1741241803; x=1741846603; darn=nongnu.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=PNVlTce1uhHhS6pAL94t07YGCV3qzSeEltruk5DehEo=; b=YStnCZx//8ZtmSQwHr4H0ypAy01Pk4rdEuQaGb4gZzi15UgIv8aPCllTdvuQPOJ2or ZPwkj+GyMgRSlNTaK68QPKQm1rbSU/j9FULyE41nSrKXsV17H3/nWHYmdLKStyW86wAA F1GOhIm123kf+6Uf9KZ/+aBPloulc0eJtdUn0Ot89PIkvCJqkjRR+7k2Py6xsS5SxxPS zTW1F2G7nykllQMfOPzc1HLw/TkbvtBjWAWwzFV+j6a7j00md/MaTcafLOwHR/d6J8um VplAn5n9dugcpeU0JaGha8Xx/LG+6xe/ThcwMuUL+mdyPYJxRsPkZaVCFzIZHLSQjc2J xSrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741241803; x=1741846603; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=PNVlTce1uhHhS6pAL94t07YGCV3qzSeEltruk5DehEo=; b=PshAZIX/EJlnQfxir1bXN4zm/qKhIaKIScnmfEeAG3Uh5xr1+D3OcTHNtEqMAk5NpU 7HFiRV9n4tRor1CpOS+MEXqBBlRjfIG4Pcwy4VmEJ2zsYFcgKd8d466Ox/xzwhxFF4Lc KQ4sZwyFrTvUKAgOZ6vIS/9FqssDiJs8vaP4AFoO7xUv5FzPz/7d7V2UuDZr0n0jrGvS TK7YLam1G8I+toHTlX9hmFDY8+5veMMAdeJR0ZaqsaZNH/io+Y9sXGOGTElawbPNoH9n eHIcB9atF8feuLfqbrh1kosNs5KYwtV5+jzmiyz4OuZ7onOhzpZwkMlYjEByFfc2NG2R UWSw== X-Gm-Message-State: AOJu0Yxd8dLypNMX5lvRT9CfjSQNsu+NC292eLRxHxXVVeU+M9VkCpds USVm9DqgtlkzAixnafocXjtFEkNunKsGv1jJDMb5xbI3ZZkbvTJGaP3Zzr5fNZw= X-Gm-Gg: ASbGncvHJR8Og6/O5a5Ry/l7YEdFvA2Kta1KEjXN8LWVQPmbndhqP1yB72wrTFxvPIx seVpZKFUJIpYugnpCGM3ku+Vbw5bOFLZSHQKIXtPLZJ5FlRLYyDhxyvDTfOFAvQs9GEFSz0DYIh B+KwzV3Ur5JV3tzn6K+NWh6ELpFPH6J1RU/vyomOepMmgigulSsfoLt7E4gQIk0Tr0QhlRbtfyh cu3wwWZJfRXHFxdlIeF7HLxLzmq6Da5eN0IKrcI6SoDKZ3CNQdYMCofnDLUUkDNw9p8a32cb2Bl FQnG70AeH/Cb/5RXrVP6ET7g/Rr5g9Sts3wVOUKimlN6L+IA X-Google-Smtp-Source: AGHT+IFjEjwNBXZq3sUdUFNGRgDCchxDULTae+EBzGiSea57VQUknE5+DPPRSgrKtIzRs6MxjfrbYw== X-Received: by 2002:a17:902:c94b:b0:21a:8300:b9ce with SMTP id d9443c01a7336-223f1d40770mr97259135ad.49.1741241803595; Wed, 05 Mar 2025 22:16:43 -0800 (PST) From: Akihiko Odaki Date: Thu, 06 Mar 2025 15:16:27 +0900 Subject: [PATCH v6 1/4] qapi: Do not consume a value if failed MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20250306-virtio-v6-1-1235eab776d9@daynix.com> References: <20250306-virtio-v6-0-1235eab776d9@daynix.com> In-Reply-To: <20250306-virtio-v6-0-1235eab776d9@daynix.com> To: Jason Wang , Dmitry Fleytman , Sriram Yagnaraman , "Michael S. Tsirkin" , Luigi Rizzo , Giuseppe Lettieri , Vincenzo Maffione , Andrew Melnychenko , Yuri Benditovich , Paolo Bonzini , =?utf-8?q?Daniel_P=2E_Berrang=C3=A9?= , Eduardo Habkost , Markus Armbruster , Michael Roth , Marcel Apfelbaum , =?utf-8?q?Philippe_Mathieu-Daud=C3=A9?= , Yanan Wang , Zhao Liu , Lei Yang , BALATON Zoltan Cc: qemu-devel@nongnu.org, devel@daynix.com, Akihiko Odaki X-Mailer: b4 0.14.2 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; Received-SPF: pass client-ip=2607:f8b0:4864:20::630; envelope-from=akihiko.odaki@daynix.com; helo=mail-pl1-x630.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @daynix-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1741241878002019000 Do not consume a value if interpreting one failed so that we can reinterpret the value with a different type. Signed-off-by: Akihiko Odaki --- qapi/qobject-input-visitor.c | 103 +++++++++++++++++++++++++++++----------= ---- 1 file changed, 69 insertions(+), 34 deletions(-) diff --git a/qapi/qobject-input-visitor.c b/qapi/qobject-input-visitor.c index c52d36997df8d99bc575c553a413df8ebcb60c32..0cc75b3c74e40a24cb1ce764330= ae2703e80d7cf 100644 --- a/qapi/qobject-input-visitor.c +++ b/qapi/qobject-input-visitor.c @@ -116,9 +116,8 @@ static const char *full_name(QObjectInputVisitor *qiv, = const char *name) return full_name_nth(qiv, name, 0); } =20 -static QObject *qobject_input_try_get_object(QObjectInputVisitor *qiv, - const char *name, - bool consume) +static QObject *qobject_input_try_get_object(const QObjectInputVisitor *qi= v, + const char *name) { StackObject *tos; QObject *qobj; @@ -138,34 +137,19 @@ static QObject *qobject_input_try_get_object(QObjectI= nputVisitor *qiv, if (qobject_type(qobj) =3D=3D QTYPE_QDICT) { assert(name); ret =3D qdict_get(qobject_to(QDict, qobj), name); - if (tos->h && consume && ret) { - bool removed =3D g_hash_table_remove(tos->h, name); - assert(removed); - } } else { assert(qobject_type(qobj) =3D=3D QTYPE_QLIST); assert(!name); - if (tos->entry) { - ret =3D qlist_entry_obj(tos->entry); - if (consume) { - tos->entry =3D qlist_next(tos->entry); - } - } else { - ret =3D NULL; - } - if (consume) { - tos->index++; - } + ret =3D tos->entry ? qlist_entry_obj(tos->entry) : NULL; } =20 return ret; } =20 static QObject *qobject_input_get_object(QObjectInputVisitor *qiv, - const char *name, - bool consume, Error **errp) + const char *name, Error **errp) { - QObject *obj =3D qobject_input_try_get_object(qiv, name, consume); + QObject *obj =3D qobject_input_try_get_object(qiv, name); =20 if (!obj) { error_setg(errp, QERR_MISSING_PARAMETER, full_name(qiv, name)); @@ -173,6 +157,38 @@ static QObject *qobject_input_get_object(QObjectInputV= isitor *qiv, return obj; } =20 +static void qobject_input_consume_object(QObjectInputVisitor *qiv, + const char *name) +{ + StackObject *tos; + QObject *qobj; + + if (QSLIST_EMPTY(&qiv->stack)) { + /* Starting at root, name is ignored. */ + return; + } + + /* We are in a container; find the next element. */ + tos =3D QSLIST_FIRST(&qiv->stack); + qobj =3D tos->obj; + assert(qobj); + + if (qobject_type(qobj) =3D=3D QTYPE_QDICT) { + assert(name); + if (tos->h) { + bool removed =3D g_hash_table_remove(tos->h, name); + assert(removed); + } + } else { + assert(qobject_type(qobj) =3D=3D QTYPE_QLIST); + assert(!name); + if (tos->entry) { + tos->entry =3D qlist_next(tos->entry); + } + tos->index++; + } +} + static const char *qobject_input_get_keyval(QObjectInputVisitor *qiv, const char *name, Error **errp) @@ -180,7 +196,7 @@ static const char *qobject_input_get_keyval(QObjectInpu= tVisitor *qiv, QObject *qobj; QString *qstr; =20 - qobj =3D qobject_input_get_object(qiv, name, true, errp); + qobj =3D qobject_input_get_object(qiv, name, errp); if (!qobj) { return NULL; } @@ -233,6 +249,7 @@ static const QListEntry *qobject_input_push(QObjectInpu= tVisitor *qiv, tos->index =3D -1; } =20 + qobject_input_consume_object(qiv, name); QSLIST_INSERT_HEAD(&qiv->stack, tos, node); return tos->entry; } @@ -279,7 +296,7 @@ static bool qobject_input_start_struct(Visitor *v, cons= t char *name, void **obj, size_t size, Error **errp) { QObjectInputVisitor *qiv =3D to_qiv(v); - QObject *qobj =3D qobject_input_get_object(qiv, name, true, errp); + QObject *qobj =3D qobject_input_get_object(qiv, name, errp); =20 if (obj) { *obj =3D NULL; @@ -316,7 +333,7 @@ static bool qobject_input_start_list(Visitor *v, const = char *name, Error **errp) { QObjectInputVisitor *qiv =3D to_qiv(v); - QObject *qobj =3D qobject_input_get_object(qiv, name, true, errp); + QObject *qobj =3D qobject_input_get_object(qiv, name, errp); const QListEntry *entry; =20 if (list) { @@ -382,7 +399,7 @@ static bool qobject_input_start_alternate(Visitor *v, c= onst char *name, Error **errp) { QObjectInputVisitor *qiv =3D to_qiv(v); - QObject *qobj =3D qobject_input_get_object(qiv, name, false, errp); + QObject *qobj =3D qobject_input_get_object(qiv, name, errp); =20 if (!qobj) { *obj =3D NULL; @@ -397,7 +414,7 @@ static bool qobject_input_type_int64(Visitor *v, const = char *name, int64_t *obj, Error **errp) { QObjectInputVisitor *qiv =3D to_qiv(v); - QObject *qobj =3D qobject_input_get_object(qiv, name, true, errp); + QObject *qobj =3D qobject_input_get_object(qiv, name, errp); QNum *qnum; =20 if (!qobj) { @@ -409,6 +426,7 @@ static bool qobject_input_type_int64(Visitor *v, const = char *name, int64_t *obj, full_name(qiv, name)); return false; } + qobject_input_consume_object(qiv, name); return true; } =20 @@ -428,6 +446,7 @@ static bool qobject_input_type_int64_keyval(Visitor *v,= const char *name, full_name(qiv, name), "integer"); return false; } + qobject_input_consume_object(qiv, name); return true; } =20 @@ -435,7 +454,7 @@ static bool qobject_input_type_uint64(Visitor *v, const= char *name, uint64_t *obj, Error **errp) { QObjectInputVisitor *qiv =3D to_qiv(v); - QObject *qobj =3D qobject_input_get_object(qiv, name, true, errp); + QObject *qobj =3D qobject_input_get_object(qiv, name, errp); QNum *qnum; int64_t val; =20 @@ -448,12 +467,14 @@ static bool qobject_input_type_uint64(Visitor *v, con= st char *name, } =20 if (qnum_get_try_uint(qnum, obj)) { + qobject_input_consume_object(qiv, name); return true; } =20 /* Need to accept negative values for backward compatibility */ if (qnum_get_try_int(qnum, &val)) { *obj =3D val; + qobject_input_consume_object(qiv, name); return true; } =20 @@ -479,6 +500,7 @@ static bool qobject_input_type_uint64_keyval(Visitor *v= , const char *name, full_name(qiv, name), "integer"); return false; } + qobject_input_consume_object(qiv, name); return true; } =20 @@ -486,7 +508,7 @@ static bool qobject_input_type_bool(Visitor *v, const c= har *name, bool *obj, Error **errp) { QObjectInputVisitor *qiv =3D to_qiv(v); - QObject *qobj =3D qobject_input_get_object(qiv, name, true, errp); + QObject *qobj =3D qobject_input_get_object(qiv, name, errp); QBool *qbool; =20 if (!qobj) { @@ -500,6 +522,7 @@ static bool qobject_input_type_bool(Visitor *v, const c= har *name, bool *obj, } =20 *obj =3D qbool_get_bool(qbool); + qobject_input_consume_object(qiv, name); return true; } =20 @@ -518,6 +541,7 @@ static bool qobject_input_type_bool_keyval(Visitor *v, = const char *name, full_name(qiv, name), "'on' or 'off'"); return false; } + qobject_input_consume_object(qiv, name); return true; } =20 @@ -525,7 +549,7 @@ static bool qobject_input_type_str(Visitor *v, const ch= ar *name, char **obj, Error **errp) { QObjectInputVisitor *qiv =3D to_qiv(v); - QObject *qobj =3D qobject_input_get_object(qiv, name, true, errp); + QObject *qobj =3D qobject_input_get_object(qiv, name, errp); QString *qstr; =20 *obj =3D NULL; @@ -540,6 +564,7 @@ static bool qobject_input_type_str(Visitor *v, const ch= ar *name, char **obj, } =20 *obj =3D g_strdup(qstring_get_str(qstr)); + qobject_input_consume_object(qiv, name); return true; } =20 @@ -549,15 +574,20 @@ static bool qobject_input_type_str_keyval(Visitor *v,= const char *name, QObjectInputVisitor *qiv =3D to_qiv(v); const char *str =3D qobject_input_get_keyval(qiv, name, errp); =20 + if (!str) { + return false; + } + *obj =3D g_strdup(str); - return !!str; + qobject_input_consume_object(qiv, name); + return true; } =20 static bool qobject_input_type_number(Visitor *v, const char *name, double= *obj, Error **errp) { QObjectInputVisitor *qiv =3D to_qiv(v); - QObject *qobj =3D qobject_input_get_object(qiv, name, true, errp); + QObject *qobj =3D qobject_input_get_object(qiv, name, errp); QNum *qnum; =20 if (!qobj) { @@ -571,6 +601,7 @@ static bool qobject_input_type_number(Visitor *v, const= char *name, double *obj, } =20 *obj =3D qnum_get_double(qnum); + qobject_input_consume_object(qiv, name); return true; } =20 @@ -593,6 +624,7 @@ static bool qobject_input_type_number_keyval(Visitor *v= , const char *name, } =20 *obj =3D val; + qobject_input_consume_object(qiv, name); return true; } =20 @@ -600,7 +632,7 @@ static bool qobject_input_type_any(Visitor *v, const ch= ar *name, QObject **obj, Error **errp) { QObjectInputVisitor *qiv =3D to_qiv(v); - QObject *qobj =3D qobject_input_get_object(qiv, name, true, errp); + QObject *qobj =3D qobject_input_get_object(qiv, name, errp); =20 *obj =3D NULL; if (!qobj) { @@ -608,6 +640,7 @@ static bool qobject_input_type_any(Visitor *v, const ch= ar *name, QObject **obj, } =20 *obj =3D qobject_ref(qobj); + qobject_input_consume_object(qiv, name); return true; } =20 @@ -615,7 +648,7 @@ static bool qobject_input_type_null(Visitor *v, const c= har *name, QNull **obj, Error **errp) { QObjectInputVisitor *qiv =3D to_qiv(v); - QObject *qobj =3D qobject_input_get_object(qiv, name, true, errp); + QObject *qobj =3D qobject_input_get_object(qiv, name, errp); =20 *obj =3D NULL; if (!qobj) { @@ -628,6 +661,7 @@ static bool qobject_input_type_null(Visitor *v, const c= har *name, return false; } *obj =3D qnull(); + qobject_input_consume_object(qiv, name); return true; } =20 @@ -647,13 +681,14 @@ static bool qobject_input_type_size_keyval(Visitor *v= , const char *name, full_name(qiv, name), "size"); return false; } + qobject_input_consume_object(qiv, name); return true; } =20 static void qobject_input_optional(Visitor *v, const char *name, bool *pre= sent) { QObjectInputVisitor *qiv =3D to_qiv(v); - QObject *qobj =3D qobject_input_try_get_object(qiv, name, false); + QObject *qobj =3D qobject_input_try_get_object(qiv, name); =20 if (!qobj) { *present =3D false; --=20 2.48.1