From nobody Sat Apr 27 06:46:33 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1548389189068487.3654892131742; Thu, 24 Jan 2019 20:06:29 -0800 (PST) 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 022AD302549; Fri, 25 Jan 2019 04:06:27 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A673C57BA; Fri, 25 Jan 2019 04:06:26 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 220323F954; Fri, 25 Jan 2019 04:06:26 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x0P45wdB013358 for ; Thu, 24 Jan 2019 23:05:58 -0500 Received: by smtp.corp.redhat.com (Postfix) id B048A57A5; Fri, 25 Jan 2019 04:05:58 +0000 (UTC) Received: from blue.redhat.com (ovpn-117-44.phx2.redhat.com [10.3.117.44]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7AE231F1 for ; Fri, 25 Jan 2019 04:05:58 +0000 (UTC) From: Eric Blake To: libvir-list@redhat.com Date: Thu, 24 Jan 2019 22:05:54 -0600 Message-Id: <20190125040555.14382-2-eblake@redhat.com> In-Reply-To: <20190125040555.14382-1-eblake@redhat.com> References: <20190125040555.14382-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 1/2] virjson: always raise vir error on append failures X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com 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.29]); Fri, 25 Jan 2019 04:06:27 +0000 (UTC) Content-Type: text/plain; charset="utf-8" A function that returns -1 for multiple possible failures, but only raises a libvirt error for some of those failures, is hard to use correctly. Yet both of our JSON object/array appenders fall in that pattern. We should either use the _QUIET memory allocation variants, and make callers decide to report failure; or we can make all failure paths noisy. This patch takes the latter approach. Signed-off-by: Eric Blake Reviewed-by: J=C3=A1n Tomko --- src/util/virjson.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/util/virjson.c b/src/util/virjson.c index 78f868a162..a028a0813a 100644 --- a/src/util/virjson.c +++ b/src/util/virjson.c @@ -620,11 +620,16 @@ virJSONValueObjectAppend(virJSONValuePtr object, { char *newkey; - if (object->type !=3D VIR_JSON_TYPE_OBJECT) + if (object->type !=3D VIR_JSON_TYPE_OBJECT) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("expecting JSON object")); return -1; + } - if (virJSONValueObjectHasKey(object, key)) + if (virJSONValueObjectHasKey(object, key)) { + virReportError(VIR_ERR_INTERNAL_ERROR, _("duplicate key '%s'"), ke= y); return -1; + } if (VIR_STRDUP(newkey, key) < 0) return -1; @@ -774,8 +779,10 @@ int virJSONValueArrayAppend(virJSONValuePtr array, virJSONValuePtr value) { - if (array->type !=3D VIR_JSON_TYPE_ARRAY) + if (array->type !=3D VIR_JSON_TYPE_ARRAY) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("expecting JSON arr= ay")); return -1; + } if (VIR_REALLOC_N(array->data.array.values, array->data.array.nvalues + 1) < 0) --=20 2.20.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Sat Apr 27 06:46:33 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1548389168308334.6250562076401; Thu, 24 Jan 2019 20:06:08 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B8EB6C002963; Fri, 25 Jan 2019 04:06:05 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E33B6103BAAD; Fri, 25 Jan 2019 04:06:04 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id E2872180339F; Fri, 25 Jan 2019 04:06:00 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x0P45xI8013363 for ; Thu, 24 Jan 2019 23:05:59 -0500 Received: by smtp.corp.redhat.com (Postfix) id 1096F57A5; Fri, 25 Jan 2019 04:05:59 +0000 (UTC) Received: from blue.redhat.com (ovpn-117-44.phx2.redhat.com [10.3.117.44]) by smtp.corp.redhat.com (Postfix) with ESMTP id CFB161F1 for ; Fri, 25 Jan 2019 04:05:58 +0000 (UTC) From: Eric Blake To: libvir-list@redhat.com Date: Thu, 24 Jan 2019 22:05:55 -0600 Message-Id: <20190125040555.14382-3-eblake@redhat.com> In-Reply-To: <20190125040555.14382-1-eblake@redhat.com> References: <20190125040555.14382-1-eblake@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 2/2] virjson: add convenience wrapper for appending string to array X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Fri, 25 Jan 2019 04:06:07 +0000 (UTC) Content-Type: text/plain; charset="utf-8" Upcoming patches need an array of strings for use in QMP block-dirty-bitmap-merge. A convenience wrapper cuts down on the verbosity of creating the array, similar to the existing virJSONValueObjectAppendString(). Signed-off-by: Eric Blake Reviewed-by: J=C3=A1n Tomko --- src/util/virjson.h | 2 ++ src/libvirt_private.syms | 1 + src/util/virjson.c | 15 +++++++++++++++ 3 files changed, 18 insertions(+) diff --git a/src/util/virjson.h b/src/util/virjson.h index a6768d904c..d815e60de9 100644 --- a/src/util/virjson.h +++ b/src/util/virjson.h @@ -138,6 +138,8 @@ int virJSONValueObjectRemoveKey(virJSONValuePtr object,= const char *key, virJSONValuePtr *value) ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); +int virJSONValueArrayAppendString(virJSONValuePtr object, const char *valu= e); + virJSONValuePtr virJSONValueFromString(const char *jsonstring); char *virJSONValueToString(virJSONValuePtr object, bool pretty); diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 89b8ca3b4f..c1f619f514 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2092,6 +2092,7 @@ virISCSIScanTargets; # util/virjson.h virJSONStringReformat; virJSONValueArrayAppend; +virJSONValueArrayAppendString; virJSONValueArrayForeachSteal; virJSONValueArrayGet; virJSONValueArraySize; diff --git a/src/util/virjson.c b/src/util/virjson.c index a028a0813a..d5d66f879f 100644 --- a/src/util/virjson.c +++ b/src/util/virjson.c @@ -795,6 +795,21 @@ virJSONValueArrayAppend(virJSONValuePtr array, } +int +virJSONValueArrayAppendString(virJSONValuePtr object, + const char *value) +{ + virJSONValuePtr jvalue =3D virJSONValueNewString(value); + if (!jvalue) + return -1; + if (virJSONValueArrayAppend(object, jvalue) < 0) { + virJSONValueFree(jvalue); + return -1; + } + return 0; +} + + int virJSONValueObjectHasKey(virJSONValuePtr object, const char *key) --=20 2.20.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list