From nobody Sun Feb 8 05:53:59 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 205.139.110.120 as permitted sender) client-ip=205.139.110.120; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zoho.com: domain of redhat.com designates 205.139.110.120 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=1571925441; cv=none; d=zoho.com; s=zohoarc; b=K+L0HKzhsnnLRBvbm2TJU+D8E0zCU4keOg6udhgvlL/aBnka+rAa0hM6vDd2OTdDdtdwQzH31xMsMSasqIb13F4PhT1P26qDjfAMdVn7zMGC1jrhi0Gf/z93EBvaZDxFbGHun85MMfBkyg0+5DcfN6Of2U2lc5hICuDLrtep6gs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zoho.com; s=zohoarc; t=1571925441; 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=p8DbW9llQSduA/OLHTT7BFTFr4IGvePk3EI/A5it0pA=; b=amCzqHSJnNEIXrki8Kyk22okigtnZ1TTPQjVQmq0B5vEc0cg1CfA2WoQbrc8d97Kdm+NwKJsrrFMutla9GVM4bshNIdA6kN1tCvTE7KytJvXfHKTsMYbA6pC2D8NcqcVjbX98igB+Dn9BLamdSoI6fju6jzX06U1bFnYG22UYSk= ARC-Authentication-Results: i=1; mx.zoho.com; dkim=pass; spf=pass (zoho.com: domain of redhat.com designates 205.139.110.120 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-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) by mx.zohomail.com with SMTPS id 157192544182041.648890016310474; Thu, 24 Oct 2019 06:57:21 -0700 (PDT) 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-358-pgCjU0WJNUuEur285upgbg-1; Thu, 24 Oct 2019 09:57:19 -0400 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 mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6C4CA100551D; Thu, 24 Oct 2019 13:57:13 +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 4441260C80; Thu, 24 Oct 2019 13:57:13 +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 01FCF1803B4C; Thu, 24 Oct 2019 13:57:12 +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 x9ODuohK026345 for ; Thu, 24 Oct 2019 09:56:50 -0400 Received: by smtp.corp.redhat.com (Postfix) id E53811000232; Thu, 24 Oct 2019 13:56:50 +0000 (UTC) Received: from angien.redhat.com (unknown [10.43.2.229]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6F2BB10027A9 for ; Thu, 24 Oct 2019 13:56:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1571925440; 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=p8DbW9llQSduA/OLHTT7BFTFr4IGvePk3EI/A5it0pA=; b=iIIkapBAmmRzCLZKaoNiulh2Bq+BZ6YXTDu6DUUyxyJNlaf6GvuLVbty53XEBtDDbJ4xk5 gjtuuFC69eFZkUBH+xrpKt6eEx7RzjxtdZ3hEqBOyBP1wT60Q4PX3YOz5VjNOkiVfwVOpZ V2I/V14iKtaK/tC/2+z7pwuW9e9Sewc= From: Peter Krempa To: libvir-list@redhat.com Date: Thu, 24 Oct 2019 15:56: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 07/19] util: buffer: Simplify handling of indent overflows 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.12 X-MC-Unique: pgCjU0WJNUuEur285upgbg-1 X-Mimecast-Spam-Score: 0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" Rather than setting usage error truncate the indentation level. Having the output string misformated is way more useful to figure out where the error lies rather than reporting an error after a giant formatter function. In testBufAutoIndent we now validate that the indentation is truncated and testBufAddBuffer2 is removed since it became bogus. Signed-off-by: Peter Krempa Reviewed-by: J=C3=A1n Tomko --- src/util/virbuffer.c | 8 ++++---- tests/virbuftest.c | 35 ++++------------------------------- 2 files changed, 8 insertions(+), 35 deletions(-) diff --git a/src/util/virbuffer.c b/src/util/virbuffer.c index 9a74ca78bb..69fcf946f5 100644 --- a/src/util/virbuffer.c +++ b/src/util/virbuffer.c @@ -56,8 +56,8 @@ virBufferSetError(virBufferPtr buf, int error) * negative to decrease). Automatic indentation is performed by all * additive functions when the existing buffer is empty or ends with a * newline (however, note that no indentation is added after newlines - * embedded in an appended string). If @indent would cause overflow, - * the buffer error indicator is set. + * embedded in an appended string). If @indent would cause overflow, the + * indentation level is truncated. */ void virBufferAdjustIndent(virBufferPtr buf, int indent) @@ -67,12 +67,12 @@ virBufferAdjustIndent(virBufferPtr buf, int indent) if (indent > 0) { if (INT_MAX - indent < buf->indent) { - virBufferSetError(buf, -1); + buf->indent =3D INT_MAX; return; } } else { if (buf->indent < -indent) { - virBufferSetError(buf, -1); + buf->indent =3D 0; return; } } diff --git a/tests/virbuftest.c b/tests/virbuftest.c index 8b8754adfa..246c572bd2 100644 --- a/tests/virbuftest.c +++ b/tests/virbuftest.c @@ -85,12 +85,12 @@ static int testBufAutoIndent(const void *data G_GNUC_UN= USED) ret =3D -1; } virBufferAdjustIndent(buf, -3); - if (virBufferGetIndent(buf, false) !=3D -1 || - virBufferGetIndent(buf, true) !=3D -1 || - virBufferError(buf) !=3D -1) { - VIR_TEST_DEBUG("Usage error not flagged"); + if (virBufferGetIndent(buf, false) !=3D 0 || + virBufferGetIndent(buf, true) !=3D 0) { + VIR_TEST_DEBUG("Indentation level not truncated"); ret =3D -1; } + virBufferAdjustIndent(buf, 3); virBufferFreeAndReset(buf); if (virBufferGetIndent(buf, false) !=3D 0 || virBufferGetIndent(buf, true) !=3D 0 || @@ -298,32 +298,6 @@ static int testBufAddBuffer(const void *data G_GNUC_UN= USED) return ret; } -static int -testBufAddBuffer2(const void *opaque G_GNUC_UNUSED) -{ - g_auto(virBuffer) buf1 =3D VIR_BUFFER_INITIALIZER; - g_auto(virBuffer) buf2 =3D VIR_BUFFER_INITIALIZER; - - /* 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"); - return -1; - } - - return 0; -} - struct testBufAddStrData { const char *data; const char *expect; @@ -481,7 +455,6 @@ 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 2.21.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list