From nobody Sun Apr 28 14:27:20 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 Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1550764237018764.9419739427711; Thu, 21 Feb 2019 07:50:37 -0800 (PST) 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 92B0481E19; Thu, 21 Feb 2019 15:50:33 +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 4F3A128558; Thu, 21 Feb 2019 15:50:33 +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 ECA103FA4A; Thu, 21 Feb 2019 15:50:32 +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 x1LFoQIV009543 for ; Thu, 21 Feb 2019 10:50:26 -0500 Received: by smtp.corp.redhat.com (Postfix) id 045AB5C204; Thu, 21 Feb 2019 15:50:26 +0000 (UTC) Received: from angien.brq.redhat.com (unknown [10.43.2.229]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7DAC15C64D for ; Thu, 21 Feb 2019 15:50:23 +0000 (UTC) From: Peter Krempa To: libvir-list@redhat.com Date: Thu, 21 Feb 2019 16:50:14 +0100 Message-Id: <4a413cc0bf9621869bd9013471ca0d6f1377204b.1550764014.git.pkrempa@redhat.com> 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 Subject: [libvirt] [PATCH 1/7] util: buffer: Remove misleading AUTOPTR func for 'virBuffer' 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.25]); Thu, 21 Feb 2019 15:50:34 +0000 (UTC) Content-Type: text/plain; charset="utf-8" 'virBufferFreeAndReset' does not free the top level structure itself. Additionally we almost exclusively use stack'd buffers rather than pointers. Signed-off-by: Peter Krempa Reviewed-by: Eric Blake --- src/util/virbuffer.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/util/virbuffer.h b/src/util/virbuffer.h index 8bf6bee644..1c8e182064 100644 --- a/src/util/virbuffer.h +++ b/src/util/virbuffer.h @@ -119,6 +119,4 @@ int virBufferGetIndent(const virBuffer *buf, bool dynam= ic); void virBufferTrim(virBufferPtr buf, const char *trim, int len); void virBufferAddStr(virBufferPtr buf, const char *str); -VIR_DEFINE_AUTOPTR_FUNC(virBuffer, virBufferFreeAndReset); - #endif /* LIBVIRT_VIRBUFFER_H */ --=20 2.20.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Sun Apr 28 14:27:20 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 Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1550845945747130.84591207527592; Fri, 22 Feb 2019 06:32:25 -0800 (PST) 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 2441E5D5FC; Fri, 22 Feb 2019 14:32:24 +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 C98C95D719; Fri, 22 Feb 2019 14:32:23 +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 8D05D181A138; Fri, 22 Feb 2019 14:32:23 +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 x1LFoUm8009567 for ; Thu, 21 Feb 2019 10:50:30 -0500 Received: by smtp.corp.redhat.com (Postfix) id B5FEF5C28E; Thu, 21 Feb 2019 15:50:30 +0000 (UTC) Received: from angien.brq.redhat.com (unknown [10.43.2.229]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3B8A166610 for ; Thu, 21 Feb 2019 15:50:26 +0000 (UTC) From: Peter Krempa To: libvir-list@redhat.com Date: Thu, 21 Feb 2019 16:50:15 +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 Subject: [libvirt] [PATCH 2/7] util: buf: Fix memory leak in virBufferEscapeN 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.39]); Fri, 22 Feb 2019 14:32:24 +0000 (UTC) Content-Type: text/plain; charset="utf-8" The conversion to VIR_AUTOFREE of 'escapeList' introduced memory leak of the copied item to be escaped: =3D=3D17517=3D=3D 2 bytes in 1 blocks are definitely lost in loss record 1 = of 32 =3D=3D17517=3D=3D at 0x483880B: malloc (vg_replace_malloc.c:309) =3D=3D17517=3D=3D by 0x54D666D: strdup (in /usr/lib64/libc-2.28.so) =3D=3D17517=3D=3D by 0x497663E: virStrdup (virstring.c:956) =3D=3D17517=3D=3D by 0x497663E: virStrdup (virstring.c:945) =3D=3D17517=3D=3D by 0x48F8853: virBufferEscapeN (virbuffer.c:707) =3D=3D17517=3D=3D by 0x403C9D: testBufEscapeN (virbuftest.c:383) =3D=3D17517=3D=3D by 0x405FA8: virTestRun (testutils.c:174) =3D=3D17517=3D=3D by 0x403A70: mymain (virbuftest.c:517) =3D=3D17517=3D=3D by 0x406BC9: virTestMain (testutils.c:1097) =3D=3D17517=3D=3D by 0x5470412: (below main) (in /usr/lib64/libc-2.28.so) [...] (all other have same backtrace as it happens in a loop) Fix it by reverting all the VIR_AUTO nonsense in this function as there is exactly one place where it's handled. This effectively reverts commits: d0a92a037123085398d4123472f59c71b436d485 96fbf6df90335c1f9315fc25162711fd632d4dab d261ed2fb1df95f6c7698b9321a82078a7335112 Signed-off-by: Peter Krempa Reviewed-by: Eric Blake --- src/util/virbuffer.c | 24 +++++------------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/src/util/virbuffer.c b/src/util/virbuffer.c index 01d71f1b59..7399bdcdfe 100644 --- a/src/util/virbuffer.c +++ b/src/util/virbuffer.c @@ -643,26 +643,11 @@ virBufferEscape(virBufferPtr buf, char escape, const = char *toescape, } -typedef struct _virBufferEscapePair virBufferEscapePair; -typedef virBufferEscapePair *virBufferEscapePairPtr; - -struct _virBufferEscapePair { +struct virBufferEscapePair { char escape; char *toescape; }; -static void -virBufferEscapePairFree(virBufferEscapePairPtr pair) -{ - if (!pair) - return; - - VIR_FREE(pair->toescape); - VIR_FREE(pair); -} - -VIR_DEFINE_AUTOPTR_FUNC(virBufferEscapePair, virBufferEscapePairFree); - /** * virBufferEscapeN: @@ -688,8 +673,8 @@ virBufferEscapeN(virBufferPtr buf, VIR_AUTOFREE(char *) escaped =3D NULL; char *out; const char *cur; - virBufferEscapePair escapeItem; - VIR_AUTOPTR(virBufferEscapePair) escapeList =3D NULL; + struct virBufferEscapePair escapeItem; + struct virBufferEscapePair *escapeList =3D NULL; size_t nescapeList =3D 0; va_list ap; @@ -704,7 +689,7 @@ virBufferEscapeN(virBufferPtr buf, va_start(ap, str); while ((escapeItem.escape =3D va_arg(ap, int))) { - if (VIR_STRDUP(escapeItem.toescape, va_arg(ap, char *)) < 0) { + if (!(escapeItem.toescape =3D va_arg(ap, char *))) { virBufferSetError(buf, errno); goto cleanup; } @@ -747,6 +732,7 @@ virBufferEscapeN(virBufferPtr buf, cleanup: va_end(ap); + VIR_FREE(escapeList); } --=20 2.20.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Sun Apr 28 14:27:20 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 Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 155076424122068.65427131781564; Thu, 21 Feb 2019 07:50:41 -0800 (PST) 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 A933E307D987; Thu, 21 Feb 2019 15:50:38 +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 771B55D71C; Thu, 21 Feb 2019 15:50:38 +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 07776181A010; Thu, 21 Feb 2019 15:50:38 +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 x1LFoZwM009587 for ; Thu, 21 Feb 2019 10:50:36 -0500 Received: by smtp.corp.redhat.com (Postfix) id F09265C290; Thu, 21 Feb 2019 15:50:35 +0000 (UTC) Received: from angien.brq.redhat.com (unknown [10.43.2.229]) by smtp.corp.redhat.com (Postfix) with ESMTP id 770EC5C659 for ; Thu, 21 Feb 2019 15:50:30 +0000 (UTC) From: Peter Krempa To: libvir-list@redhat.com Date: Thu, 21 Feb 2019 16:50:16 +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 Subject: [libvirt] [PATCH 3/7] tests: buf: Fix debug messages in 'testBufEscapeRegex' 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.48]); Thu, 21 Feb 2019 15:50:40 +0000 (UTC) Content-Type: text/plain; charset="utf-8" The messages reference testBufEscapeN instead of testBufEscapeRegex. Signed-off-by: Peter Krempa Reviewed-by: Eric Blake --- tests/virbuftest.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/virbuftest.c b/tests/virbuftest.c index 5bffe94c95..547438c646 100644 --- a/tests/virbuftest.c +++ b/tests/virbuftest.c @@ -412,12 +412,12 @@ testBufEscapeRegex(const void *opaque) virBufferEscapeRegex(&buf, "%s", data->data); if (!(actual =3D virBufferContentAndReset(&buf))) { - VIR_TEST_DEBUG("testBufEscapeN: buf is empty"); + VIR_TEST_DEBUG("testBufEscapeRegex: buf is empty"); goto cleanup; } if (STRNEQ_NULLABLE(actual, data->expect)) { - VIR_TEST_DEBUG("testBufEscapeN: Strings don't match:\n"); + VIR_TEST_DEBUG("testBufEscapeRegex: Strings don't match:\n"); virTestDifference(stderr, data->expect, actual); goto cleanup; } --=20 2.20.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Sun Apr 28 14:27:20 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 Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1550764246029483.4596643551256; Thu, 21 Feb 2019 07:50:46 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id F20115D5FC; Thu, 21 Feb 2019 15:50:43 +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 B310A60BF4; Thu, 21 Feb 2019 15:50:43 +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 5795A181A12A; Thu, 21 Feb 2019 15:50:43 +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 x1LFodYI009598 for ; Thu, 21 Feb 2019 10:50:39 -0500 Received: by smtp.corp.redhat.com (Postfix) id 6FF6F5C658; Thu, 21 Feb 2019 15:50:39 +0000 (UTC) Received: from angien.brq.redhat.com (unknown [10.43.2.229]) by smtp.corp.redhat.com (Postfix) with ESMTP id E31D55C65A for ; Thu, 21 Feb 2019 15:50:36 +0000 (UTC) From: Peter Krempa To: libvir-list@redhat.com Date: Thu, 21 Feb 2019 16:50:17 +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 Subject: [libvirt] [PATCH 4/7] util: buf: Remove virBufferEscapeN 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.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.39]); Thu, 21 Feb 2019 15:50:44 +0000 (UTC) Content-Type: text/plain; charset="utf-8" The function was used only in the tests, remove it. Signed-off-by: Peter Krempa Reviewed-by: Eric Blake --- src/libvirt_private.syms | 1 - src/util/virbuffer.c | 93 ---------------------------------------- src/util/virbuffer.h | 2 - tests/virbuftest.c | 41 ------------------ 4 files changed, 137 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index b720acdc93..aa1b2c77be 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1488,7 +1488,6 @@ virBufferContentAndReset; virBufferCurrentContent; virBufferError; virBufferEscape; -virBufferEscapeN; virBufferEscapeRegex; virBufferEscapeSexpr; virBufferEscapeShell; diff --git a/src/util/virbuffer.c b/src/util/virbuffer.c index 7399bdcdfe..12bdd13d39 100644 --- a/src/util/virbuffer.c +++ b/src/util/virbuffer.c @@ -643,99 +643,6 @@ virBufferEscape(virBufferPtr buf, char escape, const c= har *toescape, } -struct virBufferEscapePair { - char escape; - char *toescape; -}; - - -/** - * virBufferEscapeN: - * @buf: the buffer to append to - * @format: a printf like format string but with only one %s parameter - * @str: the string argument which needs to be escaped - * @...: the variable list of escape pairs - * - * The variable list of arguments @... must be composed of - * 'char escape, char *toescape' pairs followed by NULL. - * - * This has the same functionality as virBufferEscape with the extension - * that allows to specify multiple pairs of chars that needs to be escaped. - */ -void -virBufferEscapeN(virBufferPtr buf, - const char *format, - const char *str, - ...) -{ - int len; - size_t i; - VIR_AUTOFREE(char *) escaped =3D NULL; - char *out; - const char *cur; - struct virBufferEscapePair escapeItem; - struct virBufferEscapePair *escapeList =3D NULL; - size_t nescapeList =3D 0; - va_list ap; - - if ((format =3D=3D NULL) || (buf =3D=3D NULL) || (str =3D=3D NULL)) - return; - - if (buf->error) - return; - - len =3D strlen(str); - - va_start(ap, str); - - while ((escapeItem.escape =3D va_arg(ap, int))) { - if (!(escapeItem.toescape =3D va_arg(ap, char *))) { - virBufferSetError(buf, errno); - goto cleanup; - } - - if (strcspn(str, escapeItem.toescape) =3D=3D len) - continue; - - if (VIR_APPEND_ELEMENT_QUIET(escapeList, nescapeList, escapeItem) = < 0) { - virBufferSetError(buf, errno); - goto cleanup; - } - } - - if (nescapeList =3D=3D 0) { - virBufferAsprintf(buf, format, str); - goto cleanup; - } - - if (xalloc_oversized(2, len) || - VIR_ALLOC_N_QUIET(escaped, 2 * len + 1) < 0) { - virBufferSetError(buf, errno); - goto cleanup; - } - - cur =3D str; - out =3D escaped; - while (*cur !=3D 0) { - for (i =3D 0; i < nescapeList; i++) { - if (strchr(escapeList[i].toescape, *cur)) { - *out++ =3D escapeList[i].escape; - break; - } - } - *out++ =3D *cur; - cur++; - } - *out =3D 0; - - virBufferAsprintf(buf, format, escaped); - - cleanup: - va_end(ap); - VIR_FREE(escapeList); -} - - /** * virBufferURIEncodeString: * @buf: the buffer to append to diff --git a/src/util/virbuffer.h b/src/util/virbuffer.h index 1c8e182064..7e4e7645df 100644 --- a/src/util/virbuffer.h +++ b/src/util/virbuffer.h @@ -84,8 +84,6 @@ void virBufferStrcatVArgs(virBufferPtr buf, va_list ap); void virBufferEscape(virBufferPtr buf, char escape, const char *toescape, const char *format, const char *str); -void virBufferEscapeN(virBufferPtr buf, const char *format, - const char *str, ...); void virBufferEscapeString(virBufferPtr buf, const char *format, const char *str); void virBufferEscapeSexpr(virBufferPtr buf, const char *format, diff --git a/tests/virbuftest.c b/tests/virbuftest.c index 547438c646..bdb0a5e934 100644 --- a/tests/virbuftest.c +++ b/tests/virbuftest.c @@ -372,35 +372,6 @@ testBufEscapeStr(const void *opaque ATTRIBUTE_UNUSED) } -static int -testBufEscapeN(const void *opaque) -{ - const struct testBufAddStrData *data =3D opaque; - virBuffer buf =3D VIR_BUFFER_INITIALIZER; - char *actual; - int ret =3D -1; - - virBufferEscapeN(&buf, "%s", data->data, '\\', "=3D", ',', ",", NULL); - - if (!(actual =3D virBufferContentAndReset(&buf))) { - VIR_TEST_DEBUG("testBufEscapeN: buf is empty"); - goto cleanup; - } - - if (STRNEQ_NULLABLE(actual, data->expect)) { - VIR_TEST_DEBUG("testBufEscapeN: Strings don't match:\n"); - virTestDifference(stderr, data->expect, actual); - goto cleanup; - } - - ret =3D 0; - - cleanup: - VIR_FREE(actual); - return ret; -} - - static int testBufEscapeRegex(const void *opaque) { @@ -506,18 +477,6 @@ mymain(void) DO_TEST_ESCAPE("\x01\x01\x02\x03\x05\x08", "\n \n"); -#define DO_TEST_ESCAPEN(data, expect) \ - do { \ - struct testBufAddStrData info =3D { data, expect }; \ - if (virTestRun("Buf: EscapeN", testBufEscapeN, &info) < 0) \ - ret =3D -1; \ - } while (0) - - DO_TEST_ESCAPEN("noescape", "noescape"); - DO_TEST_ESCAPEN("comma,escape", "comma,,escape"); - DO_TEST_ESCAPEN("equal=3Descape", "equal\\=3Descape"); - DO_TEST_ESCAPEN("comma,equal=3Descape", "comma,,equal\\=3Descape"); - #define DO_TEST_ESCAPE_REGEX(data, expect) \ do { \ struct testBufAddStrData info =3D { data, expect }; \ --=20 2.20.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Sun Apr 28 14:27:20 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 Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1550764257247210.79231977074426; Thu, 21 Feb 2019 07:50:57 -0800 (PST) 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 17ADA30B4779; Thu, 21 Feb 2019 15:50:53 +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 CD4895D9D3; Thu, 21 Feb 2019 15:50:52 +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 5FDB4181A12C; Thu, 21 Feb 2019 15:50:52 +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 x1LFooUp009628 for ; Thu, 21 Feb 2019 10:50:50 -0500 Received: by smtp.corp.redhat.com (Postfix) id 72F955C28E; Thu, 21 Feb 2019 15:50:50 +0000 (UTC) Received: from angien.brq.redhat.com (unknown [10.43.2.229]) by smtp.corp.redhat.com (Postfix) with ESMTP id ECA765C650 for ; Thu, 21 Feb 2019 15:50:39 +0000 (UTC) From: Peter Krempa To: libvir-list@redhat.com Date: Thu, 21 Feb 2019 16:50:18 +0100 Message-Id: <0721cf8ffa31ba778f474500bdd5e794fc7871a3.1550764014.git.pkrempa@redhat.com> 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 Subject: [libvirt] [PATCH 5/7] util: alloc: Introduce 'VIR_AUTOCLEAN' macros for clearing stack'd structs 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.46]); Thu, 21 Feb 2019 15:50:55 +0000 (UTC) Content-Type: text/plain; charset="utf-8" The new utility macros are useful for variables we put on the stack but require some cleanup. The most prominent of those is virBuffer which is used almost exclusively in that way. Signed-off-by: Peter Krempa Reviewed-by: Eric Blake --- src/util/viralloc.h | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/util/viralloc.h b/src/util/viralloc.h index 1d67fff95c..705c2636fa 100644 --- a/src/util/viralloc.h +++ b/src/util/viralloc.h @@ -613,7 +613,24 @@ void virAllocTestHook(void (*func)(int, void*), void *= data); if (*_ptr) \ (func)(*_ptr); \ *_ptr =3D NULL; \ - } \ + } + +# define VIR_AUTOCLEAN_FUNC_NAME(type) type##AutoClean + +/** + * VIR_DEFINE_AUTOCLEAN_FUNC: + * @type: type of the variable to be cleared automatically + * @func: cleanup function to be automatically called + * + * This macro defines a function for automatic clearing of + * resources in a stack'd variable of type @type. This newly + * defined function works as a necessary wrapper around @func. + */ +# define VIR_DEFINE_AUTOCLEAN_FUNC(type, func) \ + static inline void VIR_AUTOCLEAN_FUNC_NAME(type)(type *_ptr) \ + { \ + (func)(_ptr); \ + } /** * VIR_AUTOFREE: @@ -637,6 +654,19 @@ void virAllocTestHook(void (*func)(int, void*), void *= data); # define VIR_AUTOPTR(type) \ __attribute__((cleanup(VIR_AUTOPTR_FUNC_NAME(type)))) type * +/** + * VIR_AUTOCLEAN: + * @type: type of the variable to be cleared + * + * Macro to automatically calls destructor of @type variable declared dire= ctly + * when the variable goes out of scope. + * The destructor is defined by VIR_DEFINE_AUTOCLEAN_FUNC macro for the gi= ven + * type. + */ +# define VIR_AUTOCLEAN(type) \ + __attribute__((cleanup(VIR_AUTOCLEAN_FUNC_NAME(type)))) type + + /** * VIR_AUTOUNREF: * @type: type of an virObject subclass to be unref'd automatically --=20 2.20.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Sun Apr 28 14:27:20 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 Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1550845999223135.53450161644446; Fri, 22 Feb 2019 06:33:19 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id E00F430B824C; Fri, 22 Feb 2019 14:33:16 +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 A34351001DD4; Fri, 22 Feb 2019 14:33:16 +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 451AD247E1; Fri, 22 Feb 2019 14:33:16 +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 x1LFovPg009678 for ; Thu, 21 Feb 2019 10:50:57 -0500 Received: by smtp.corp.redhat.com (Postfix) id C89DF17119; Thu, 21 Feb 2019 15:50:57 +0000 (UTC) Received: from angien.brq.redhat.com (unknown [10.43.2.229]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4DD7917110 for ; Thu, 21 Feb 2019 15:50:50 +0000 (UTC) From: Peter Krempa To: libvir-list@redhat.com Date: Thu, 21 Feb 2019 16:50:19 +0100 Message-Id: <9d88a85d224997ba4e32d324d5df59308f252431.1550764014.git.pkrempa@redhat.com> 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 Subject: [libvirt] [PATCH 6/7] util: buffer: Introduce VIR_AUTOCLEAN function for virBuffer 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.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.44]); Fri, 22 Feb 2019 14:33:18 +0000 (UTC) Content-Type: text/plain; charset="utf-8" virBuffer is almost always stack-allocated, but requires freeing of the internals on error. Introduce a VIR_AUTOCLEAN function to deal with this. Along with the addition add a test which would leak the buffer contents if it weren't autocleaned. Signed-off-by: Peter Krempa Reviewed-by: Eric Blake --- src/util/virbuffer.h | 3 +++ tests/virbuftest.c | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/util/virbuffer.h b/src/util/virbuffer.h index 7e4e7645df..b399c90154 100644 --- a/src/util/virbuffer.h +++ b/src/util/virbuffer.h @@ -59,6 +59,9 @@ int virBufferCheckErrorInternal(const virBuffer *buf, const char *funcname, size_t linenr) ATTRIBUTE_NONNULL(1); + +VIR_DEFINE_AUTOCLEAN_FUNC(virBuffer, virBufferFreeAndReset); + /** * virBufferCheckError * diff --git a/tests/virbuftest.c b/tests/virbuftest.c index bdb0a5e934..34f02b1281 100644 --- a/tests/virbuftest.c +++ b/tests/virbuftest.c @@ -429,6 +429,17 @@ testBufSetIndent(const void *opaque ATTRIBUTE_UNUSED) } +/* Result of this shows up only in valgrind or similar */ +static int +testBufferAutoclean(const void *opaque ATTRIBUTE_UNUSED) +{ + VIR_AUTOCLEAN(virBuffer) buf =3D VIR_BUFFER_INITIALIZER; + + virBufferAddLit(&buf, "test test test\n"); + return 0; +} + + static int mymain(void) { @@ -448,6 +459,7 @@ mymain(void) DO_TEST("Trim", testBufTrim, 0); DO_TEST("AddBuffer", testBufAddBuffer, 0); DO_TEST("set indent", testBufSetIndent, 0); + DO_TEST("autoclean", testBufferAutoclean, 0); #define DO_TEST_ADD_STR(DATA, EXPECT) \ do { \ --=20 2.20.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Sun Apr 28 14:27:20 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 Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1550846006369293.0376665183567; Fri, 22 Feb 2019 06:33:26 -0800 (PST) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id AF44230BCC1B; Fri, 22 Feb 2019 14:33:24 +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 7B79760185; Fri, 22 Feb 2019 14:33:24 +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 345DE181A00A; Fri, 22 Feb 2019 14:33:24 +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 x1LFp0t5009718 for ; Thu, 21 Feb 2019 10:51:00 -0500 Received: by smtp.corp.redhat.com (Postfix) id B7EB217104; Thu, 21 Feb 2019 15:51:00 +0000 (UTC) Received: from angien.brq.redhat.com (unknown [10.43.2.229]) by smtp.corp.redhat.com (Postfix) with ESMTP id 36BD45C707 for ; Thu, 21 Feb 2019 15:50:57 +0000 (UTC) From: Peter Krempa To: libvir-list@redhat.com Date: Thu, 21 Feb 2019 16:50:20 +0100 Message-Id: <1be13a33b0e684ed4197958a793a68072f43232d.1550764014.git.pkrempa@redhat.com> 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 Subject: [libvirt] [PATCH 7/7] qemu: domain: Use VIR_AUTOCLEAN for virBuffer 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.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.44]); Fri, 22 Feb 2019 14:33:25 +0000 (UTC) Content-Type: text/plain; charset="utf-8" Replace all uses where virBuffer would need clearing on the cleanup path. Signed-off-by: Peter Krempa Reviewed-by: Eric Blake --- src/qemu/qemu_domain.c | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index bbd4a5efe8..8682b27037 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -2174,7 +2174,7 @@ static int qemuStorageSourcePrivateDataFormat(virStorageSourcePtr src, virBufferPtr buf) { - virBuffer tmp =3D VIR_BUFFER_INITIALIZER; + VIR_AUTOCLEAN(virBuffer) tmp =3D VIR_BUFFER_INITIALIZER; qemuDomainStorageSourcePrivatePtr srcPriv =3D QEMU_DOMAIN_STORAGE_SOUR= CE_PRIVATE(src); int ret =3D -1; @@ -2209,7 +2209,6 @@ qemuStorageSourcePrivateDataFormat(virStorageSourcePt= r src, ret =3D 0; cleanup: - virBufferFreeAndReset(&tmp); return ret; } @@ -2345,9 +2344,9 @@ static int qemuDomainObjPrivateXMLFormatNBDMigrationSource(virBufferPtr buf, virStorageSourcePtr src) { - virBuffer attrBuf =3D VIR_BUFFER_INITIALIZER; - virBuffer childBuf =3D VIR_BUFFER_INITIALIZER; - virBuffer privateDataBuf =3D VIR_BUFFER_INITIALIZER; + VIR_AUTOCLEAN(virBuffer) attrBuf =3D VIR_BUFFER_INITIALIZER; + VIR_AUTOCLEAN(virBuffer) childBuf =3D VIR_BUFFER_INITIALIZER; + VIR_AUTOCLEAN(virBuffer) privateDataBuf =3D VIR_BUFFER_INITIALIZER; int ret =3D -1; virBufferSetChildIndent(&childBuf, buf); @@ -2373,10 +2372,6 @@ qemuDomainObjPrivateXMLFormatNBDMigrationSource(virB= ufferPtr buf, ret =3D 0; cleanup: - virBufferFreeAndReset(&attrBuf); - virBufferFreeAndReset(&childBuf); - virBufferFreeAndReset(&privateDataBuf); - return ret; } @@ -2385,8 +2380,8 @@ static int qemuDomainObjPrivateXMLFormatNBDMigration(virBufferPtr buf, virDomainObjPtr vm) { - virBuffer attrBuf =3D VIR_BUFFER_INITIALIZER; - virBuffer childBuf =3D VIR_BUFFER_INITIALIZER; + VIR_AUTOCLEAN(virBuffer) attrBuf =3D VIR_BUFFER_INITIALIZER; + VIR_AUTOCLEAN(virBuffer) childBuf =3D VIR_BUFFER_INITIALIZER; size_t i; virDomainDiskDefPtr disk; qemuDomainDiskPrivatePtr diskPriv; @@ -2413,9 +2408,6 @@ qemuDomainObjPrivateXMLFormatNBDMigration(virBufferPt= r buf, ret =3D 0; cleanup: - virBufferFreeAndReset(&attrBuf); - virBufferFreeAndReset(&childBuf); - return ret; } @@ -2425,8 +2417,8 @@ qemuDomainObjPrivateXMLFormatJob(virBufferPtr buf, virDomainObjPtr vm, qemuDomainObjPrivatePtr priv) { - virBuffer attrBuf =3D VIR_BUFFER_INITIALIZER; - virBuffer childBuf =3D VIR_BUFFER_INITIALIZER; + VIR_AUTOCLEAN(virBuffer) attrBuf =3D VIR_BUFFER_INITIALIZER; + VIR_AUTOCLEAN(virBuffer) childBuf =3D VIR_BUFFER_INITIALIZER; qemuDomainJob job =3D priv->job.active; int ret =3D -1; @@ -2465,9 +2457,6 @@ qemuDomainObjPrivateXMLFormatJob(virBufferPtr buf, ret =3D 0; cleanup: - virBufferFreeAndReset(&attrBuf); - virBufferFreeAndReset(&childBuf); - return ret; } --=20 2.20.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list