From nobody Mon Feb 9 20:37:01 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=1640175794; cv=none; d=zohomail.com; s=zohoarc; b=QxdZ5zX7c3+NNhBaclsTvlDM+OK6A6TumQ1PKh0dpwb/jLni8npS7MY0aUnD9t3yeeKE6UwMmImzMuGodNPPi9Lmxerx1KPb7taE9fh7bifrhyQVSE3CBKLiEtQ2zl/e36ImaIYOsJBb36oD0nLFzfJA8txinv7bbldZNAVjf+E= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1640175794; 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=xxm5A6pfbprpREeSurRbzEfksIKWWy9UNRQtJ/ZWQgE=; b=LPc1y6xzcrYRZqV4bL1PSYaX6CwnIYt5vJbHZpc5GKXOcZ+bIRqIIMb0FwrcgKlpiSx/ivuJhp/bFQGoUFrwSe2Pki5tZ2VoO+VtwXmsN85eNIo7GKu5wUNZG8vtzi6DnzQtq2/URqPZvlJcZEk6EBI6p6gupIEQAwdQXKkwtEo= 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 1640175794771643.3382950338455; Wed, 22 Dec 2021 04:23:14 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-587-UPcC7pEkNf-2LBKu0vk52A-1; Wed, 22 Dec 2021 07:22:02 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 204B285EE69; Wed, 22 Dec 2021 12:21:57 +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 EE88C5F913; Wed, 22 Dec 2021 12:21:56 +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 5D1131806D2B; Wed, 22 Dec 2021 12:21:56 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 1BMC3Uk1008420 for ; Wed, 22 Dec 2021 07:03:30 -0500 Received: by smtp.corp.redhat.com (Postfix) id 0DED17314B; Wed, 22 Dec 2021 12:03:30 +0000 (UTC) Received: from speedmetal.redhat.com (unknown [10.40.208.10]) by smtp.corp.redhat.com (Postfix) with ESMTP id 60113709AA for ; Wed, 22 Dec 2021 12:03:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1640175793; 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=xxm5A6pfbprpREeSurRbzEfksIKWWy9UNRQtJ/ZWQgE=; b=eZENOItUbU1gML9DRK3FhdFcFjOW3oPbopIIqLX9R6Z3QAJzLHtxL1xSH+bOQfrt0VPj7j ftFOqnFm0ygFx7elUhhNFNsUBhIXMl+rclxydxi5dlYsuMzNAF/ry6O/5np7NejhXo4S/F K06+CVEKGo579Lg0mGV6ewwXBjy7XEE= X-MC-Unique: UPcC7pEkNf-2LBKu0vk52A-1 From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 3/6] util: json: Don't copy string in virJSONValueNewString Date: Wed, 22 Dec 2021 13:02:39 +0100 Message-Id: <854c4680219cd8e5b15d7c82513888613708bf25.1640174526.git.pkrempa@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 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.14 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: 1640175796042100001 Content-Type: text/plain; charset="utf-8" With 'g_strdup' not needing error handling we can ask callers to pass a copy of the string which will be adopted by the JSON value. Signed-off-by: Peter Krempa --- src/util/virjson.c | 17 +++++++---------- src/util/virjson.h | 2 +- tests/virjsontest.c | 4 ++-- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/util/virjson.c b/src/util/virjson.c index dbc5b7840c..37301bb87e 100644 --- a/src/util/virjson.c +++ b/src/util/virjson.c @@ -412,7 +412,7 @@ virJSONValueHashFree(void *opaque) virJSONValue * -virJSONValueNewString(const char *data) +virJSONValueNewString(char *data) { virJSONValue *val; @@ -422,7 +422,7 @@ virJSONValueNewString(const char *data) val =3D g_new0(virJSONValue, 1); val->type =3D VIR_JSON_TYPE_STRING; - val->data.string =3D g_strdup(data); + val->data.string =3D data; return val; } @@ -608,12 +608,9 @@ virJSONValueObjectInsertString(virJSONValue *object, const char *value, bool prepend) { - virJSONValue *jvalue =3D virJSONValueNewString(value); - if (!jvalue) - return -1; - if (virJSONValueObjectInsert(object, key, &jvalue, prepend) < 0) - return -1; - return 0; + g_autoptr(virJSONValue) jvalue =3D virJSONValueNewString(g_strdup(valu= e)); + + return virJSONValueObjectInsert(object, key, &jvalue, prepend); } @@ -775,7 +772,7 @@ int virJSONValueArrayAppendString(virJSONValue *object, const char *value) { - g_autoptr(virJSONValue) jvalue =3D virJSONValueNewString(value); + g_autoptr(virJSONValue) jvalue =3D virJSONValueNewString(g_strdup(valu= e)); if (virJSONValueArrayAppend(object, &jvalue) < 0) return -1; @@ -1438,7 +1435,7 @@ virJSONValueCopy(const virJSONValue *in) /* No need to error out in the following cases */ case VIR_JSON_TYPE_STRING: - out =3D virJSONValueNewString(in->data.string); + out =3D virJSONValueNewString(g_strdup(in->data.string)); break; case VIR_JSON_TYPE_NUMBER: out =3D virJSONValueNewNumber(g_strdup(in->data.number)); diff --git a/src/util/virjson.h b/src/util/virjson.h index 361b0190e8..71b88183fe 100644 --- a/src/util/virjson.h +++ b/src/util/virjson.h @@ -50,7 +50,7 @@ int virJSONValueObjectAddVArgs(virJSONValue **objptr, va_= list args) ATTRIBUTE_NONNULL(1); -virJSONValue *virJSONValueNewString(const char *data); +virJSONValue *virJSONValueNewString(char *data); virJSONValue *virJSONValueNewStringLen(const char *data, size_t length); virJSONValue *virJSONValueNewNumberInt(int data); virJSONValue *virJSONValueNewNumberUint(unsigned int data); diff --git a/tests/virjsontest.c b/tests/virjsontest.c index ddb93803b7..78283b632a 100644 --- a/tests/virjsontest.c +++ b/tests/virjsontest.c @@ -441,8 +441,8 @@ testJSONObjectFormatSteal(const void *opaque G_GNUC_UNU= SED) g_autoptr(virJSONValue) t1 =3D NULL; g_autoptr(virJSONValue) t2 =3D NULL; - if (!(a1 =3D virJSONValueNewString("test")) || - !(a2 =3D virJSONValueNewString("test"))) { + if (!(a1 =3D virJSONValueNewString(g_strdup("test"))) || + !(a2 =3D virJSONValueNewString(g_strdup("test")))) { VIR_TEST_VERBOSE("Failed to create json object"); } --=20 2.31.1