From nobody Tue Feb 10 11:14:35 2026 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