From nobody Tue Feb 10 10:59:14 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) client-ip=63.128.21.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 63.128.21.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=1613152658; cv=none; d=zohomail.com; s=zohoarc; b=Lzrju5SBrlyOqyNHDM+mMA9XKTnOu1qY+OlFyQbk5rpwwlAGE2/aPNBwiVxSmOSJKilxtDMJVETBoWEXk7+RbjMvv5niXcLcIpiltKGvGqcWlXmLxb927zyogmgUiHJAgFoTbvpa3eGqTCWu0ojBUIptV/zz7lZkbJycReMvioo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1613152658; 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=fcPt+q0OF0RPdo/gkfVhRGo+3z4FXx1y4gplscOffdo=; b=daVl1Nxm0iqLnT2yMKkfAKbQjr60xLIEUJkZ3Dlw0eFPttHcsnHFi5OvDtzmRrFWzCuteb73mk79xbn22wc9p9ZbB5byMIHnhEXG6HnO5GOmmxucjgz+45WSED3QzX4qgND6M7NXZTCks4ha+Q/RnmrwkhSwNpcXMFBl81aF+ko= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 63.128.21.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by mx.zohomail.com with SMTPS id 1613152658329773.4889325355613; Fri, 12 Feb 2021 09:57:38 -0800 (PST) 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-158-ex1fguKSOHymv6u8EXzc7A-1; Fri, 12 Feb 2021 12:57:34 -0500 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 49890C2B7; Fri, 12 Feb 2021 17:57:07 +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 22C1F5D6AB; Fri, 12 Feb 2021 17:57:07 +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 E001758075; Fri, 12 Feb 2021 17:57:06 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 11CHuoo5011048 for ; Fri, 12 Feb 2021 12:56:50 -0500 Received: by smtp.corp.redhat.com (Postfix) id 290AA5C23D; Fri, 12 Feb 2021 17:56:50 +0000 (UTC) Received: from speedmetal.lan (unknown [10.40.208.53]) by smtp.corp.redhat.com (Postfix) with ESMTP id 767605C3E0 for ; Fri, 12 Feb 2021 17:56:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1613152656; 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=fcPt+q0OF0RPdo/gkfVhRGo+3z4FXx1y4gplscOffdo=; b=YlTFBgd8gDoVj4YFa5lSMX9xBiwWsV3LXswn+Aj5dgz1Uc6ZnPMq4P3iJMEaWB086JgQ6K 59F7YeTZP+2jS6GaioqnWs7+pNN7OeKcuKGqwIg45LMNfWw873Uso7WAPm8tcAzq6jI4fM fg7B5cFOISQyNoqTeezGu2NzXB6TfTg= X-MC-Unique: ex1fguKSOHymv6u8EXzc7A-1 From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 24/25] virJSONValueNewNumber: Take ownership of passed string Date: Fri, 12 Feb 2021 18:55:37 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 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.15 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) Content-Type: text/plain; charset="utf-8" Avoid pointless copies of temporary strings when constructing number JSON objects. Signed-off-by: Peter Krempa --- src/util/virjson.c | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/src/util/virjson.c b/src/util/virjson.c index de83441e27..b21b1fc63f 100644 --- a/src/util/virjson.c +++ b/src/util/virjson.c @@ -465,15 +465,22 @@ virJSONValueNewStringLen(const char *data, } +/** + * virJSONValueNewNumber: + * @data: string representing the number + * + * Creates a new virJSONValue of VIR_JSON_TYPE_NUMBER type. Note that this + * function takes ownership of @data. + */ static virJSONValuePtr -virJSONValueNewNumber(const char *data) +virJSONValueNewNumber(char *data) { virJSONValuePtr val; val =3D g_new0(virJSONValue, 1); val->type =3D VIR_JSON_TYPE_NUMBER; - val->data.number =3D g_strdup(data); + val->data.number =3D data; return val; } @@ -482,43 +489,35 @@ virJSONValueNewNumber(const char *data) virJSONValuePtr virJSONValueNewNumberInt(int data) { - g_autofree char *str =3D NULL; - str =3D g_strdup_printf("%i", data); - return virJSONValueNewNumber(str); + return virJSONValueNewNumber(g_strdup_printf("%i", data)); } virJSONValuePtr virJSONValueNewNumberUint(unsigned int data) { - g_autofree char *str =3D NULL; - str =3D g_strdup_printf("%u", data); - return virJSONValueNewNumber(str); + return virJSONValueNewNumber(g_strdup_printf("%u", data)); } virJSONValuePtr virJSONValueNewNumberLong(long long data) { - g_autofree char *str =3D NULL; - str =3D g_strdup_printf("%lld", data); - return virJSONValueNewNumber(str); + return virJSONValueNewNumber(g_strdup_printf("%lld", data)); } virJSONValuePtr virJSONValueNewNumberUlong(unsigned long long data) { - g_autofree char *str =3D NULL; - str =3D g_strdup_printf("%llu", data); - return virJSONValueNewNumber(str); + return virJSONValueNewNumber(g_strdup_printf("%llu", data)); } virJSONValuePtr virJSONValueNewNumberDouble(double data) { - g_autofree char *str =3D NULL; + char *str =3D NULL; if (virDoubleToStr(&str, data) < 0) return NULL; return virJSONValueNewNumber(str); @@ -1534,7 +1533,7 @@ virJSONValueCopy(const virJSONValue *in) out =3D virJSONValueNewString(in->data.string); break; case VIR_JSON_TYPE_NUMBER: - out =3D virJSONValueNewNumber(in->data.number); + out =3D virJSONValueNewNumber(g_strdup(in->data.number)); break; case VIR_JSON_TYPE_BOOLEAN: out =3D virJSONValueNewBoolean(in->data.boolean); @@ -1639,10 +1638,9 @@ virJSONParserHandleNumber(void *ctx, size_t l) { virJSONParserPtr parser =3D ctx; - g_autofree char *str =3D g_strndup(s, l); - g_autoptr(virJSONValue) value =3D virJSONValueNewNumber(str); + g_autoptr(virJSONValue) value =3D virJSONValueNewNumber(g_strndup(s, l= )); - VIR_DEBUG("parser=3D%p str=3D%s", parser, str); + VIR_DEBUG("parser=3D%p str=3D%s", parser, value->data.number); if (virJSONParserInsertValue(parser, value) < 0) return 0; --=20 2.29.2