From nobody Mon Feb 9 13:37:25 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.129.124 as permitted sender) client-ip=170.10.129.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.129.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=1633622696; cv=none; d=zohomail.com; s=zohoarc; b=VwCg+2Hn8DF53Daonx21tCluiChUkunwrQ6/huthV2fIfg9SBMDFSwhrfy6xnQntr/AKXprrh8C4x6JRIG6Avwe9FwKZilLBKiwGw7TrkptNk++X9riJXphKs8VnQ0Ort3EppIy+dhFU+3c1+GGGrpro9ap1C7zPECPNKMyZKoM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1633622696; 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=4S7pfBLh59LEArleKidfQ4AiDYm3s4pLfH6rxj2/Ujg=; b=g59ZYVojkYJZJ5lSCWTn/RairCB8qJqoZMWV6Ydp4lz96wfRyd/mmHZ+NyC7THNYON2wgU4iRS3p20JEBxdgTLusxs4nrHx7z5N51snK8jw0KdG+LZDvgFzEsqvJHV/gyGuHsjD2p0RRtOn8cWEe9ujM+8jOHgoKnRpAJ+NvCeI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.129.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.129.124]) by mx.zohomail.com with SMTPS id 1633622695913321.68857140037767; Thu, 7 Oct 2021 09:04:55 -0700 (PDT) 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-33-_jtFCWBSPv6muqSp_OVvtQ-1; Thu, 07 Oct 2021 12:04:53 -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 7CAAB1B132DD; Thu, 7 Oct 2021 15:46:09 +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 589775E454; Thu, 7 Oct 2021 15:45:44 +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 2649C1803B30; Thu, 7 Oct 2021 15:45:44 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 197FIaIf023510 for ; Thu, 7 Oct 2021 11:18:36 -0400 Received: by smtp.corp.redhat.com (Postfix) id A7CE95D6D5; Thu, 7 Oct 2021 15:18:36 +0000 (UTC) Received: from speedmetal.redhat.com (unknown [10.40.208.5]) by smtp.corp.redhat.com (Postfix) with ESMTP id 0BE785D6BA for ; Thu, 7 Oct 2021 15:18:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1633622695; 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=4S7pfBLh59LEArleKidfQ4AiDYm3s4pLfH6rxj2/Ujg=; b=NowyjJt2cnA6V5AlujPVIl7szRFV9XzwoKSs/qUFjYluO6+g6f367DysR0sNjyx2R//Z9M eApVA7Y6feaa3P+4Xqk35mHFqh/hoEGQd/TqAfYFwM3uaJJ1yifQRJ0xVl+qjNQ12LrGIN 7jCuVOZ+6g0K26qwhubB5X++jTUHCGE= X-MC-Unique: _jtFCWBSPv6muqSp_OVvtQ-1 From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 003/103] virJSONValueObjectAddVArgs: Add new convertors for allocated strings Date: Thu, 7 Oct 2021 17:16:51 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: libvir-list@redhat.com 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com 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: 1633622698479100001 Content-Type: text/plain; charset="utf-8" The 'f' and 'F' convertors add a string value to the JSON value object and free the passed pointer. This is helpful in case we need to e.g. add single use formatted values: virJSONValueObjectAdd(props, "f:test1", g_strdup_printf("%s-test1", blah), "F:test2", virGetStringAndReportErrorOnNull(), NULL); With 'F' if the passed string is NULL no additional error is reported so the error is passed through. To prevent leaking the strings on reasonable failures we add an 'err' variable which skips the implementation of the conversions, but keeps iterating through the arguments. On hard programming errors (bad key format) the string is leaked but that should not be a problem in practice. Signed-off-by: Peter Krempa --- src/util/virjson.c | 82 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 68 insertions(+), 14 deletions(-) diff --git a/src/util/virjson.c b/src/util/virjson.c index d7e72af8f9..4d9cd5629a 100644 --- a/src/util/virjson.c +++ b/src/util/virjson.c @@ -112,6 +112,8 @@ virJSONValueGetType(const virJSONValue *value) * Explanation of type codes: * s: string value, must be non-null * S: string value, omitted if null + * f: string value, passed pointer is freed, error reported if NULL + * F: string value, passed pointer is freed, no error reported if NULL * * i: signed integer value * j: signed integer value, error if negative @@ -155,6 +157,7 @@ int virJSONValueObjectAddVArgs(virJSONValue *obj, va_list args) { + bool err =3D false; char type; char *key; int rc; @@ -173,17 +176,30 @@ virJSONValueObjectAddVArgs(virJSONValue *obj, /* This doesn't support maps, but no command uses those. */ switch (type) { + case 'f': + case 'F': case 'S': case 's': { char *val =3D va_arg(args, char *); + g_autofree char *valf =3D NULL; + + if (type =3D=3D 'f' || type =3D=3D 'F') + valf =3D val; + + if (err) + continue; + if (!val) { if (type =3D=3D 'S') continue; - virReportError(VIR_ERR_INTERNAL_ERROR, - _("argument key '%s' must not have null val= ue"), - key); - return -1; + if (type !=3D 'F') { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("argument key '%s' must not have null= value"), + key); + } + err =3D true; + continue; } rc =3D virJSONValueObjectAppendString(obj, key, val); } break; @@ -195,11 +211,15 @@ virJSONValueObjectAddVArgs(virJSONValue *obj, case 'i': { int val =3D va_arg(args, int); + if (err) + continue; + if (val < 0 && (type =3D=3D 'j' || type =3D=3D 'y')) { virReportError(VIR_ERR_INTERNAL_ERROR, _("argument key '%s' must not be negative"), key); - return -1; + err =3D true; + continue; } if (val =3D=3D 0 && (type =3D=3D 'z' || type =3D=3D 'y')) @@ -215,6 +235,9 @@ virJSONValueObjectAddVArgs(virJSONValue *obj, case 'u': { unsigned int val =3D va_arg(args, unsigned int); + if (err) + continue; + if (!val && type =3D=3D 'p') continue; @@ -227,11 +250,15 @@ virJSONValueObjectAddVArgs(virJSONValue *obj, case 'I': { long long val =3D va_arg(args, long long); + if (err) + continue; + if (val < 0 && (type =3D=3D 'J' || type =3D=3D 'Y')) { virReportError(VIR_ERR_INTERNAL_ERROR, _("argument key '%s' must not be negative"), key); - return -1; + err =3D true; + continue; } if (!val && (type =3D=3D 'Z' || type =3D=3D 'Y')) @@ -249,6 +276,9 @@ virJSONValueObjectAddVArgs(virJSONValue *obj, */ long long val =3D va_arg(args, long long); + if (err) + continue; + if (!val && type =3D=3D 'P') continue; @@ -257,6 +287,10 @@ virJSONValueObjectAddVArgs(virJSONValue *obj, case 'd': { double val =3D va_arg(args, double); + + if (err) + continue; + rc =3D virJSONValueObjectAppendNumberDouble(obj, key, val); } break; @@ -265,6 +299,9 @@ virJSONValueObjectAddVArgs(virJSONValue *obj, case 'b': { int val =3D va_arg(args, int); + if (err) + continue; + if (!val && type =3D=3D 'B') continue; @@ -282,6 +319,9 @@ virJSONValueObjectAddVArgs(virJSONValue *obj, } break; case 'n': { + if (err) + continue; + rc =3D virJSONValueObjectAppendNull(obj, key); } break; @@ -289,6 +329,9 @@ virJSONValueObjectAddVArgs(virJSONValue *obj, case 'a': { virJSONValue **val =3D va_arg(args, virJSONValue **); + if (err) + continue; + if (!(*val)) { if (type =3D=3D 'A') continue; @@ -296,7 +339,8 @@ virJSONValueObjectAddVArgs(virJSONValue *obj, virReportError(VIR_ERR_INTERNAL_ERROR, _("argument key '%s' must not have null val= ue"), key); - return -1; + err =3D true; + continue; } rc =3D virJSONValueObjectAppend(obj, key, val); @@ -308,6 +352,9 @@ virJSONValueObjectAddVArgs(virJSONValue *obj, g_autoptr(virJSONValue) jsonMap =3D virJSONValueNewArray(); ssize_t pos =3D -1; + if (err) + continue; + if (!map) { if (type =3D=3D 'M') continue; @@ -315,18 +362,20 @@ virJSONValueObjectAddVArgs(virJSONValue *obj, virReportError(VIR_ERR_INTERNAL_ERROR, _("argument key '%s' must not have null val= ue"), key); - return -1; + err =3D true; + continue; } while ((pos =3D virBitmapNextSetBit(map, pos)) > -1) { g_autoptr(virJSONValue) newelem =3D virJSONValueNewNumberL= ong(pos); - if (virJSONValueArrayAppend(jsonMap, &newelem) < 0) - return -1; + if (virJSONValueArrayAppend(jsonMap, &newelem) < 0) { + err =3D true; + continue; + } } - if ((rc =3D virJSONValueObjectAppend(obj, key, &jsonMap)) < 0) - return -1; + rc =3D virJSONValueObjectAppend(obj, key, &jsonMap); } break; default: @@ -335,10 +384,15 @@ virJSONValueObjectAddVArgs(virJSONValue *obj, return -1; } - if (rc < 0) - return -1; + if (rc < 0) { + err =3D true; + continue; + } } + if (err) + return -1; + /* verify that we added at least one key-value pair */ if (virJSONValueObjectKeysNumber(obj) =3D=3D 0) return 0; --=20 2.31.1