From nobody Sun Feb 8 23:25:58 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1669912328; cv=none; d=zohomail.com; s=zohoarc; b=ZnP10aTcmAFES0Pj7lXWevim4spfGG5daSIWLEoaozvNtOEPz6/cOXJPj3tZuH2NrogVsbAkRm0X0SYmYYL3ZCujzECB70eWedqFPK7+V2l8jaTFhrKdcyxGuPxehXlCOlIkGGLCzVB7/Qw2BP8Km+MB4vrbIhLw79GlPiDOzWY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1669912328; h=Content-Type:Content-Transfer-Encoding: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=+vzAoRQOH2r1e6UDQYhVTDJT1l0Di0W5Mj3cyyQKjBI=; b=oAkX3Ld+4mVE+y6dmPJQVQcqXfK+cgpNr3MHjHyW93wsRa9+812UieoFDVVaT1H1meN6eKERFtkL7FjbiglSJR86JhVreUcmoue4pYDoe5bRLBb9Itn5yhZb4T4oG0Oy95zgwoSoflnaJoaE2EDe54oU3TtW+7NbNX6KdtDnTtU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1669912328788437.389889566684; Thu, 1 Dec 2022 08:32:08 -0800 (PST) Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-191-TANfkVDzPV6TH-vGM5rJNQ-1; Thu, 01 Dec 2022 11:31:39 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 75C7E8828CF; Thu, 1 Dec 2022 16:31:25 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com [10.30.29.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 5ED6020290A2; Thu, 1 Dec 2022 16:31:25 +0000 (UTC) Received: from mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (localhost [IPv6:::1]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 4488119465A8; Thu, 1 Dec 2022 16:31:25 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) by mm-prod-listman-01.mail-001.prod.us-east-1.aws.redhat.com (Postfix) with ESMTP id 911B119465A8 for ; Thu, 1 Dec 2022 16:31:24 +0000 (UTC) Received: by smtp.corp.redhat.com (Postfix) id 7506FC1D3AD; Thu, 1 Dec 2022 16:31:24 +0000 (UTC) Received: from speedmetal.redhat.com (ovpn-208-20.brq.redhat.com [10.40.208.20]) by smtp.corp.redhat.com (Postfix) with ESMTP id D5A47C2DF60 for ; Thu, 1 Dec 2022 16:31:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1669912327; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=+vzAoRQOH2r1e6UDQYhVTDJT1l0Di0W5Mj3cyyQKjBI=; b=SLWbzq4NWbQ+ECLzrA1fXH5mfyRPNbFrGrgSqhYoykJsoX9JpIKjAgEYzxwawjjh0Qv5tQ o948RqNurDbYVIiM4ldVQULe1ZcnnkMVjs0VXP0oJabO1guTCo2QpCRL/kpLnNIWHAn9fY hDcMSUNKZB5UXUGuj4LXbJZqrblo1S0= X-MC-Unique: TANfkVDzPV6TH-vGM5rJNQ-1 X-Original-To: libvir-list@listman.corp.redhat.com From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 06/12] util: json: Split out array->strinlist conversion from virJSONValueObjectGetStringArray Date: Thu, 1 Dec 2022 17:31:10 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libvir-list-bounces@redhat.com Sender: "libvir-list" X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1669912329079100001 Content-Type: text/plain; charset="utf-8" Introduce virJSONValueArrayToStringList which does only the conversion from an array to a stringlist. This will allow refactoring the callers to be more careful in case when they want to handle the existance of the member in the parent object differently. Signed-off-by: Peter Krempa --- src/libvirt_private.syms | 1 + src/util/virjson.c | 43 ++++++++++++++++++++++------------------ src/util/virjson.h | 2 ++ 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index ebd7bc61a8..a5bb44a9f8 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2564,6 +2564,7 @@ virJSONValueArrayForeachSteal; virJSONValueArrayGet; virJSONValueArraySize; virJSONValueArraySteal; +virJSONValueArrayToStringList; virJSONValueCopy; virJSONValueFree; virJSONValueFromString; diff --git a/src/util/virjson.c b/src/util/virjson.c index ef59b5deb4..5701c09bfd 100644 --- a/src/util/virjson.c +++ b/src/util/virjson.c @@ -1316,10 +1316,7 @@ virJSONValueObjectStealObject(virJSONValue *object, char ** virJSONValueObjectGetStringArray(virJSONValue *object, const char *key) { - g_auto(GStrv) ret =3D NULL; virJSONValue *data; - size_t n; - size_t i; data =3D virJSONValueObjectGetArray(object, key); if (!data) { @@ -1329,32 +1326,40 @@ virJSONValueObjectGetStringArray(virJSONValue *obje= ct, const char *key) return NULL; } - n =3D virJSONValueArraySize(data); - ret =3D g_new0(char *, n + 1); + return virJSONValueArrayToStringList(data); +} + + +/** + * virJSONValueArrayToStringList: + * @data: a JSON array containing strings to convert + * + * Converts @data a JSON array containing strings to a NULL-terminated str= ing + * list. @data must be a JSON array. In case @data is doesn't contain only + * strings an error is reported. + */ +char ** +virJSONValueArrayToStringList(virJSONValue *data) +{ + size_t n =3D virJSONValueArraySize(data); + g_auto(GStrv) ret =3D g_new0(char *, n + 1); + size_t i; + for (i =3D 0; i < n; i++) { virJSONValue *child =3D virJSONValueArrayGet(data, i); - const char *tmp; - if (!child) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("%s array element is missing item %zu"), - key, i); + if (!child || + !(ret[i] =3D g_strdup(virJSONValueGetString(child)))) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("JSON string array contains non-string elemen= t")); return NULL; } - - if (!(tmp =3D virJSONValueGetString(child))) { - virReportError(VIR_ERR_INTERNAL_ERROR, - _("%s array element does not contain a string"), - key); - return NULL; - } - - ret[i] =3D g_strdup(tmp); } return g_steal_pointer(&ret); } + /** * virJSONValueObjectForeachKeyValue: * @object: JSON object to iterate diff --git a/src/util/virjson.h b/src/util/virjson.h index ce181dcb82..49a89e0910 100644 --- a/src/util/virjson.h +++ b/src/util/virjson.h @@ -172,6 +172,8 @@ virJSONValueObjectGetString(virJSONValue *object, char ** virJSONValueObjectGetStringArray(virJSONValue *object, const char *key); +char ** +virJSONValueArrayToStringList(virJSONValue *data); const char * virJSONValueObjectGetStringOrNumber(virJSONValue *object, const char *key); --=20 2.38.1