From nobody Wed May 1 22:21:23 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 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=1555589500; cv=none; d=zoho.com; s=zohoarc; b=VwS/61bRbNbMijja7pUKy9+jUUVIkbt8Z4c2ovfi0G1Ircm6a6Q7OL0uH574zMQfO0z8GC6BmxLnJF1avhHllOP4bvwkO3bRR8BqzTX1hQRokT+SoLqimeus/SCVnVgurhmC4Q/OMZDMeCm970pLDIQuzv++WCtXsR8lGhp/ta8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1555589500; 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:ARC-Authentication-Results; bh=8JgxDScqc1yb4qP/L9cw2WCjYCZan98fd2JaS13ollg=; b=W2YGy9Z6LOAQFf3O3oiChWekhqKNz8bYTISRtEn/MVV51EOp65gkDDftXpvFO9KgJ//7rI898FnxNwne6UewuqA++5q7e7VAGLVb2rPVVY7YvacD4enmdR6FPsmtDCwt9E29yC7ADJa/TnzQY3295P1dFi10boz+gv4dg6yiXRc= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1555589500214296.36739915006956; Thu, 18 Apr 2019 05:11:40 -0700 (PDT) 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 mx1.redhat.com (Postfix) with ESMTPS id 7862D308A10F; Thu, 18 Apr 2019 12:11:36 +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 AE4DA5D706; Thu, 18 Apr 2019 12:11:35 +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 502FF41F3E; Thu, 18 Apr 2019 12:11:33 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x3ICBUgK016254 for ; Thu, 18 Apr 2019 08:11:30 -0400 Received: by smtp.corp.redhat.com (Postfix) id 999241001DE1; Thu, 18 Apr 2019 12:11:30 +0000 (UTC) Received: from moe.brq.redhat.com (unknown [10.43.2.30]) by smtp.corp.redhat.com (Postfix) with ESMTP id 21E241001E98 for ; Thu, 18 Apr 2019 12:11:29 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Thu, 18 Apr 2019 14:11:23 +0200 Message-Id: <66e5b174ecadf5ea5f3d782b2890c349936ea005.1555589456.git.mprivozn@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 1/3] virbuffer: Don't leak memory in virBufferAddBuffer 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: , Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.44]); Thu, 18 Apr 2019 12:11:37 +0000 (UTC) Content-Type: text/plain; charset="utf-8" If an error occurs in a virBuffer* API the idea is to free the content immediately and set @error member used in error reporting later. Well, this is not what how virBufferAddBuffer works. Signed-off-by: Michal Privoznik Reviewed-by: Erik Skultety --- src/util/virbuffer.c | 2 +- tests/virbuftest.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/util/virbuffer.c b/src/util/virbuffer.c index 54703a28d8..b2ae7963a1 100644 --- a/src/util/virbuffer.c +++ b/src/util/virbuffer.c @@ -197,7 +197,7 @@ virBufferAddBuffer(virBufferPtr buf, virBufferPtr toadd) =20 if (buf->error || toadd->error) { if (!buf->error) - buf->error =3D toadd->error; + virBufferSetError(buf, toadd->error); goto done; } =20 diff --git a/tests/virbuftest.c b/tests/virbuftest.c index 778754d7c1..bdd0c16462 100644 --- a/tests/virbuftest.c +++ b/tests/virbuftest.c @@ -303,6 +303,37 @@ static int testBufAddBuffer(const void *data ATTRIBUTE= _UNUSED) return ret; } =20 +static int +testBufAddBuffer2(const void *opaque ATTRIBUTE_UNUSED) +{ + virBuffer buf1 =3D VIR_BUFFER_INITIALIZER; + virBuffer buf2 =3D VIR_BUFFER_INITIALIZER; + int ret =3D -1; + + /* Intent of this test is to demonstrate a memleak that happen with + * virBufferAddBuffer */ + + virBufferAddLit(&buf1, "Hello world!\n"); + virBufferAddLit(&buf2, "Hello world!\n"); + + /* Intentional usage error */ + virBufferAdjustIndent(&buf2, -2); + + virBufferAddBuffer(&buf1, &buf2); + + if (virBufferCurrentContent(&buf1) || + !virBufferCurrentContent(&buf2)) { + VIR_TEST_DEBUG("Unexpected buffer content"); + goto cleanup; + } + + ret =3D 0; + cleanup: + virBufferFreeAndReset(&buf1); + virBufferFreeAndReset(&buf2); + return ret; +} + struct testBufAddStrData { const char *data; const char *expect; @@ -460,6 +491,7 @@ mymain(void) DO_TEST("Auto-indentation", testBufAutoIndent, 0); DO_TEST("Trim", testBufTrim, 0); DO_TEST("AddBuffer", testBufAddBuffer, 0); + DO_TEST("AddBuffer2", testBufAddBuffer2, 0); DO_TEST("set indent", testBufSetIndent, 0); DO_TEST("autoclean", testBufferAutoclean, 0); =20 --=20 2.21.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Wed May 1 22:21:23 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 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=1555589516; cv=none; d=zoho.com; s=zohoarc; b=N7G2/6cQ1Gevv31WZqiflNjlvKHiPE7C9+1Gu7+jNTCgseVNVjyoAI0LEyr67Zyy82Gk4IfNSZg75sKJtmjhnQf2V7EvfXe5UU53rSMP+1NAQFdJl3aPJys21+AeijqNXAAWIC/HI3ERkT0sQYBjw81+xW/5c3wdg/qWF75lxtU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1555589516; 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:ARC-Authentication-Results; bh=vLXPRJphKIUv6CK4tnaNt86gHJzn4J4ZdY3t9XSzosA=; b=jh+iBLGfV2J2VjWFCupp1HGp18A5zZ3/5m7gS9baTEV8ZyO0CZGFtEt5r4N7NWUMUrvzaLPSgrnizROqUGUl5ClKjYAF9+AGuxGj9n25+NHLxmZyFvPTI4iMGozuKBoBqk6yrQQivZpS8zXJi+Xq/yV6WP4te8mgO9K7t4w+y4U= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1555589516592510.40093977068807; Thu, 18 Apr 2019 05:11:56 -0700 (PDT) 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 mx1.redhat.com (Postfix) with ESMTPS id 8D51C88ABF; Thu, 18 Apr 2019 12:11:54 +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 674395D9C5; Thu, 18 Apr 2019 12:11:54 +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 27211181AC45; Thu, 18 Apr 2019 12:11:54 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x3ICBV7D016262 for ; Thu, 18 Apr 2019 08:11:31 -0400 Received: by smtp.corp.redhat.com (Postfix) id 69A9F1001E98; Thu, 18 Apr 2019 12:11:31 +0000 (UTC) Received: from moe.brq.redhat.com (unknown [10.43.2.30]) by smtp.corp.redhat.com (Postfix) with ESMTP id E72171001DE1 for ; Thu, 18 Apr 2019 12:11:30 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Thu, 18 Apr 2019 14:11:24 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 2/3] virbuffer: Use signed integer for storing error 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: , Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Thu, 18 Apr 2019 12:11:55 +0000 (UTC) Content-Type: text/plain; charset="utf-8" The @error member can contain a positive value (errno) or a negative value (-1) to denote an usage error. It doesn't make much sense to store it as unsigned then. Signed-off-by: Michal Privoznik Reviewed-by: Erik Skultety --- src/util/virbuffer.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/virbuffer.h b/src/util/virbuffer.h index fbfed5cbf3..dd9d4ab3d4 100644 --- a/src/util/virbuffer.h +++ b/src/util/virbuffer.h @@ -40,7 +40,7 @@ typedef virBuffer *virBufferPtr; struct _virBuffer { size_t size; size_t use; - unsigned int error; /* errno value, or -1 for usage error */ + int error; /* errno value, or -1 for usage error */ int indent; char *content; }; --=20 2.21.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Wed May 1 22:21:23 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 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=1555589520; cv=none; d=zoho.com; s=zohoarc; b=NbK5lo2sDemHu18JvvSedgyHNdGAghLicyJe3m/3ImfBhj+feF1uyMLkCA9osjRzLXTxnPltmKVxAothFfA1TLrND6wH5QEBvivlfWHf+2tj6dhHvjVAtph7WJKqjjgp61K7BSOPwK2J3TXaMwdOEas4qjczJqQsHxN7s68FpQg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1555589520; 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:ARC-Authentication-Results; bh=AfleJsixuUnJ74l8zDf1lCkE2ksfEKBSL06Xv9hah44=; b=HFEoI8tJ1O0Gl1YqzePucPN7Gmjum8SrR8iT1MePsocwO5g0nTPfVcB1iy7OMDaK650ywmn3IOQjCvfNH8pLr5rxaA7/e6kvwCkh/Q3vk9jDe/tD/NnbAbzX1iSx6FUUkFKKzng60OZrZogQBv/4z6vA7j68+dOJFGmTCBXQxCY= ARC-Authentication-Results: i=1; mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1555589520608646.9158153947183; Thu, 18 Apr 2019 05:12:00 -0700 (PDT) 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 mx1.redhat.com (Postfix) with ESMTPS id 0096F44F88; Thu, 18 Apr 2019 12:11:58 +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 CEF0719C7B; Thu, 18 Apr 2019 12:11:57 +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 957D73FB12; Thu, 18 Apr 2019 12:11:57 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id x3ICBWnC016270 for ; Thu, 18 Apr 2019 08:11:32 -0400 Received: by smtp.corp.redhat.com (Postfix) id 3E4221001E9A; Thu, 18 Apr 2019 12:11:32 +0000 (UTC) Received: from moe.brq.redhat.com (unknown [10.43.2.30]) by smtp.corp.redhat.com (Postfix) with ESMTP id B7EF21001E98 for ; Thu, 18 Apr 2019 12:11:31 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Thu, 18 Apr 2019 14:11:25 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 3/3] virBuffer: Try harder to free buffer 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: , Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Thu, 18 Apr 2019 12:11:59 +0000 (UTC) Content-Type: text/plain; charset="utf-8" Currently, the way virBufferFreeAndReset() works is it relies on virBufferContentAndReset() to fetch the buffer content which is then freed. This works as long as there is no bug in virBuffer* implementation (not true apparently). Explicitly call free() over buffer content. Signed-off-by: Michal Privoznik Reviewed-by: Erik Skultety --- src/util/virbuffer.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/util/virbuffer.c b/src/util/virbuffer.c index b2ae7963a1..ac03b15a61 100644 --- a/src/util/virbuffer.c +++ b/src/util/virbuffer.c @@ -281,9 +281,8 @@ virBufferContentAndReset(virBufferPtr buf) */ void virBufferFreeAndReset(virBufferPtr buf) { - char *str =3D virBufferContentAndReset(buf); - - VIR_FREE(str); + if (buf) + virBufferSetError(buf, 0); } =20 /** --=20 2.21.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list