From nobody Tue Feb 10 07:40:31 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=1613152685; cv=none; d=zohomail.com; s=zohoarc; b=W/meqnR6YI8jb/T2lPvXuQLiQ1oVuP+27MZPK/FmYBfUzXtDKcKQz2k8OWuTqbQMvLML8z/wOb2JxjG1gY8Nay7PbxwEvJsAT1lnWSeuXS3bgpKV1u9VXH7sIxTRzdcNOuRYUZ0fIAZiLm4gENiwC/+pizuD0a8wXLi9HJ6lRkM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1613152685; 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=kfef1BUW9QAqturOTOC5b4MoH521/ZhvEyxYjYXIdZM=; b=QMsDOAUtX8BF/3WDZU3FemXwL2MOLIaX6tIOCuOJs4dPPKBWfCUQalfkDg145heUPs34g0xHxoXEG1EJn9j55eu4F8uHMi20ArTIi7sTLc1cAFTPedPQPq2leswRlptm1ox/Ar+EklagWBGAh+EQ+FO13BYC9axOttuon4/gkrU= 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 1613152684697547.276234120364; Fri, 12 Feb 2021 09:58:04 -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-306-M9p0yBqEPY6R5gGCVUkdNg-1; Fri, 12 Feb 2021 12:57:36 -0500 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 mimecast-mx01.redhat.com (Postfix) with ESMTPS id E747D188579E; Fri, 12 Feb 2021 17:57:03 +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 BDB9419D6C; Fri, 12 Feb 2021 17:57:03 +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 73DC958075; Fri, 12 Feb 2021 17:57:03 +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 11CHunDd011038 for ; Fri, 12 Feb 2021 12:56:49 -0500 Received: by smtp.corp.redhat.com (Postfix) id 02FDB5C3E0; Fri, 12 Feb 2021 17:56:49 +0000 (UTC) Received: from speedmetal.lan (unknown [10.40.208.53]) by smtp.corp.redhat.com (Postfix) with ESMTP id 492235C23D for ; Fri, 12 Feb 2021 17:56:48 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1613152682; 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=kfef1BUW9QAqturOTOC5b4MoH521/ZhvEyxYjYXIdZM=; b=BeVwrH52eGy1V8Gw8thTBTBTBmGPYqsEolMSgJLCDp738lkgz3SSymFKZz2dedWD9KAYWs AzAXx0+zuNR39j+tR3biYDAHkeApe8SgdviY6OV3auIKk/gReAFpy6XEQe1+wzCvctY9Mh 2vZmJJkPFfCzvt4ump3pjIoaEGmYjr8= X-MC-Unique: M9p0yBqEPY6R5gGCVUkdNg-1 From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 23/25] virJSONParserHandle*: Refactor memory cleanup and drop NULL checks Date: Fri, 12 Feb 2021 18:55:36 +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.84 on 10.5.11.23 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" virJSONValueNew* won't return error nowadays so NULL checks are not necessary. The memory can be cleared via g_autoptr. Signed-off-by: Peter Krempa --- src/util/virjson.c | 68 ++++++++++++++++------------------------------ 1 file changed, 24 insertions(+), 44 deletions(-) diff --git a/src/util/virjson.c b/src/util/virjson.c index 65215f03c5..de83441e27 100644 --- a/src/util/virjson.c +++ b/src/util/virjson.c @@ -1604,17 +1604,13 @@ static int virJSONParserHandleNull(void *ctx) { virJSONParserPtr parser =3D ctx; - virJSONValuePtr value =3D virJSONValueNewNull(); + g_autoptr(virJSONValue) value =3D virJSONValueNewNull(); VIR_DEBUG("parser=3D%p", parser); - if (!value) + if (virJSONParserInsertValue(parser, value) < 0) return 0; - - if (virJSONParserInsertValue(parser, value) < 0) { - virJSONValueFree(value); - return 0; - } + value =3D NULL; return 1; } @@ -1625,17 +1621,13 @@ virJSONParserHandleBoolean(void *ctx, int boolean_) { virJSONParserPtr parser =3D ctx; - virJSONValuePtr value =3D virJSONValueNewBoolean(boolean_); + g_autoptr(virJSONValue) value =3D virJSONValueNewBoolean(boolean_); VIR_DEBUG("parser=3D%p boolean=3D%d", parser, boolean_); - if (!value) - return 0; - - if (virJSONParserInsertValue(parser, value) < 0) { - virJSONValueFree(value); + if (virJSONParserInsertValue(parser, value) < 0) return 0; - } + value =3D NULL; return 1; } @@ -1647,22 +1639,14 @@ virJSONParserHandleNumber(void *ctx, size_t l) { virJSONParserPtr parser =3D ctx; - char *str; - virJSONValuePtr value; - - str =3D g_strndup(s, l); - value =3D virJSONValueNewNumber(str); - VIR_FREE(str); + g_autofree char *str =3D g_strndup(s, l); + g_autoptr(virJSONValue) value =3D virJSONValueNewNumber(str); VIR_DEBUG("parser=3D%p str=3D%s", parser, str); - if (!value) + if (virJSONParserInsertValue(parser, value) < 0) return 0; - - if (virJSONParserInsertValue(parser, value) < 0) { - virJSONValueFree(value); - return 0; - } + value =3D NULL; return 1; } @@ -1674,18 +1658,14 @@ virJSONParserHandleString(void *ctx, size_t stringLen) { virJSONParserPtr parser =3D ctx; - virJSONValuePtr value =3D virJSONValueNewStringLen((const char *)strin= gVal, - stringLen); + g_autoptr(virJSONValue) value =3D virJSONValueNewStringLen((const char= *)stringVal, + stringLen); VIR_DEBUG("parser=3D%p str=3D%p", parser, (const char *)stringVal); - if (!value) - return 0; - - if (virJSONParserInsertValue(parser, value) < 0) { - virJSONValueFree(value); + if (virJSONParserInsertValue(parser, value) < 0) return 0; - } + value =3D NULL; return 1; } @@ -1716,21 +1696,21 @@ static int virJSONParserHandleStartMap(void *ctx) { virJSONParserPtr parser =3D ctx; - virJSONValuePtr value =3D virJSONValueNewObject(); + g_autoptr(virJSONValue) value =3D virJSONValueNewObject(); + virJSONValuePtr tmp =3D value; VIR_DEBUG("parser=3D%p", parser); - if (virJSONParserInsertValue(parser, value) < 0) { - virJSONValueFree(value); + if (virJSONParserInsertValue(parser, value) < 0) return 0; - } + value =3D NULL; if (VIR_REALLOC_N(parser->state, parser->nstate + 1) < 0) { return 0; } - parser->state[parser->nstate].value =3D value; + parser->state[parser->nstate].value =3D tmp; parser->state[parser->nstate].key =3D NULL; parser->nstate++; @@ -1765,20 +1745,20 @@ static int virJSONParserHandleStartArray(void *ctx) { virJSONParserPtr parser =3D ctx; - virJSONValuePtr value =3D virJSONValueNewArray(); + g_autoptr(virJSONValue) value =3D virJSONValueNewArray(); + virJSONValuePtr tmp =3D value; VIR_DEBUG("parser=3D%p", parser); - if (virJSONParserInsertValue(parser, value) < 0) { - virJSONValueFree(value); + if (virJSONParserInsertValue(parser, value) < 0) return 0; - } + value =3D NULL; if (VIR_REALLOC_N(parser->state, parser->nstate + 1) < 0) return 0; - parser->state[parser->nstate].value =3D value; + parser->state[parser->nstate].value =3D tmp; parser->state[parser->nstate].key =3D NULL; parser->nstate++; --=20 2.29.2