From nobody Mon May 6 10:46:36 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) client-ip=205.139.110.61; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from us-smtp-delivery-1.mimecast.com (us-smtp-1.mimecast.com [205.139.110.61]) by mx.zohomail.com with SMTPS id 1580673215653178.72451556218903; Sun, 2 Feb 2020 11:53:35 -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-294-ghvsvSyPMECoNGpaa0pEOQ-1; Sun, 02 Feb 2020 14:53:32 -0500 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 mimecast-mx01.redhat.com (Postfix) with ESMTPS id 0FD4D800D4E; Sun, 2 Feb 2020 19:53:27 +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 3219C1BC6D; Sun, 2 Feb 2020 19:53: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 E6D241809567; Sun, 2 Feb 2020 19:53:19 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 012JrHBX003202 for ; Sun, 2 Feb 2020 14:53:17 -0500 Received: by smtp.corp.redhat.com (Postfix) id C343E87B1D; Sun, 2 Feb 2020 19:53:17 +0000 (UTC) Received: from icr.redhat.com (unknown [10.43.2.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4C52086C4A for ; Sun, 2 Feb 2020 19:53:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1580673214; 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=gSE8RlikE074iij1hr7kIvZQx0ENUUnkPDLOOyniTPs=; b=EF4fqZBwxIPWrJITCj8xswC1m7v2HZg0z4z13BGpRpAbcS6T9MS9dd1So/0iZMAGM3K9fG UC3Ih23xbkv8yS3DdEw1Ww/Z3ZjczARVW6FI1/7xk1KVSJuUfJJk/IPNcuUi0SV4xLKZKk QDto6c+Wyr5NrYSXm9FVEhADnOCiM2k= From: =?UTF-8?q?J=C3=A1n=20Tomko?= To: libvir-list@redhat.com Subject: [libvirt PATCH 1/3] util: introduce virBufferTrimLen Date: Sun, 2 Feb 2020 20:53:02 +0100 Message-Id: <21dc7c483734587883d0b6dee16a5c3e7c2d53ef.1580673055.git.jtomko@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 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.79 on 10.5.11.15 X-MC-Unique: ghvsvSyPMECoNGpaa0pEOQ-1 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" Just like the existing virBufferTrim, but only does one thing at a time. Signed-off-by: J=C3=A1n Tomko Reviewed-by: Peter Krempa --- src/libvirt_private.syms | 1 + src/util/virbuffer.c | 19 +++++++++++++++++++ src/util/virbuffer.h | 1 + 3 files changed, 21 insertions(+) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index ebf830791e..7dfb2458eb 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1646,6 +1646,7 @@ virBufferStrcat; virBufferStrcatVArgs; virBufferTrim; virBufferTrimChars; +virBufferTrimLen; virBufferURIEncodeString; virBufferUse; virBufferVasprintf; diff --git a/src/util/virbuffer.c b/src/util/virbuffer.c index 914c386b18..b76d99b56f 100644 --- a/src/util/virbuffer.c +++ b/src/util/virbuffer.c @@ -700,6 +700,25 @@ virBufferTrimChars(virBufferPtr buf, const char *trim) g_string_truncate(buf->str, i + 1); } =20 +/** + * virBufferTrimLen: + * @buf: the buffer to trim + * @len: the number of bytes to trim + * + * Trim the tail of a buffer. + */ +void +virBufferTrimLen(virBufferPtr buf, int len) +{ + if (!buf || !buf->str) + return; + + if (len > buf->str->len) + return; + + g_string_truncate(buf->str, buf->str->len - len); +} + /** * virBufferAddStr: * @buf: the buffer to append to diff --git a/src/util/virbuffer.h b/src/util/virbuffer.h index 183f78f279..7b068075b2 100644 --- a/src/util/virbuffer.h +++ b/src/util/virbuffer.h @@ -93,4 +93,5 @@ size_t virBufferGetEffectiveIndent(const virBuffer *buf); =20 void virBufferTrim(virBufferPtr buf, const char *trim, int len); void virBufferTrimChars(virBufferPtr buf, const char *trim); +void virBufferTrimLen(virBufferPtr buf, int len); void virBufferAddStr(virBufferPtr buf, const char *str); --=20 2.19.2 From nobody Mon May 6 10:46:36 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) client-ip=207.211.31.120; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) by mx.zohomail.com with SMTPS id 1580673242184311.63848007755143; Sun, 2 Feb 2020 11:54:02 -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-63-St6JtwecNN67L1ILnAVXvQ-1; Sun, 02 Feb 2020 14:53:58 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 28DD418FE860; Sun, 2 Feb 2020 19:53: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 E70845C1D4; Sun, 2 Feb 2020 19:53: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 AAD2E18089CF; Sun, 2 Feb 2020 19:53:52 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 012JrIHX003212 for ; Sun, 2 Feb 2020 14:53:18 -0500 Received: by smtp.corp.redhat.com (Postfix) id 9525B87B1D; Sun, 2 Feb 2020 19:53:18 +0000 (UTC) Received: from icr.redhat.com (unknown [10.43.2.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1C03C86C4A for ; Sun, 2 Feb 2020 19:53:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1580673241; 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=qj5TtY8s3MeI61txhMiYmn804ISJJ+4mEYBzdjvj3cg=; b=MZA1dts0lIE0xZffVKyuGnh/gvF9P4eP972a8S8d/3Tbf2TM09s/qt6HW3VzcD9ugM0Wid hKUYFdlwfCGyLlAoNN6mFLJGFjtBY2MMyl7rwUpXHfgWXqUkvPj0WRJFVEeVeJLBQLLbpj +Z8GEIu3bRTkeRruvw2LlR/pl9CYZDI= From: =?UTF-8?q?J=C3=A1n=20Tomko?= To: libvir-list@redhat.com Subject: [libvirt PATCH 2/3] Use virBufferTrimLen when applicable Date: Sun, 2 Feb 2020 20:53:03 +0100 Message-Id: <4abca449c08e2edb8c2d59fd4fda7995c196fd3d.1580673055.git.jtomko@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 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.79 on 10.5.11.16 X-MC-Unique: St6JtwecNN67L1ILnAVXvQ-1 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" Replace all the cases that only supply the length and do not care about matching a suffix, as well as that one test case that does. Signed-off-by: J=C3=A1n Tomko Reviewed-by: Peter Krempa --- src/lxc/lxc_container.c | 2 +- tests/virbuftest.c | 6 +++--- tools/vsh.c | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c index b44cc68e67..fcd964cd1e 100644 --- a/src/lxc/lxc_container.c +++ b/src/lxc/lxc_container.c @@ -215,7 +215,7 @@ static virCommandPtr lxcContainerBuildInitCmd(virDomain= DefPtr vmDef, virBufferAdd(&buf, ttyPaths[i] + 5, -1); virBufferAddChar(&buf, ' '); } - virBufferTrim(&buf, NULL, 1); + virBufferTrimLen(&buf, 1); =20 virUUIDFormat(vmDef->uuid, uuidstr); =20 diff --git a/tests/virbuftest.c b/tests/virbuftest.c index 7919075000..f2d4d9b9de 100644 --- a/tests/virbuftest.c +++ b/tests/virbuftest.c @@ -108,9 +108,9 @@ static int testBufTrim(const void *data G_GNUC_UNUSED) virBufferAddLit(buf, "a;"); virBufferTrim(buf, "", 0); virBufferTrim(buf, "", -1); - virBufferTrim(buf, NULL, 1); - virBufferTrim(buf, NULL, 5); - virBufferTrim(buf, "a", 2); + virBufferTrimLen(buf, 1); + virBufferTrimLen(buf, 5); + virBufferTrimLen(buf, 2); =20 virBufferAddLit(buf, ",b,,"); virBufferTrim(buf, NULL, -1); diff --git a/tools/vsh.c b/tools/vsh.c index 9c58e40d4f..3c3b4bf1c3 100644 --- a/tools/vsh.c +++ b/tools/vsh.c @@ -2525,7 +2525,7 @@ vshTreePrintInternal(vshControl *ctl, vshPrint(ctl, "%s\n", virBufferCurrentContent(indent)); =20 if (!root) - virBufferTrim(indent, NULL, 2); + virBufferTrimLen(indent, 2); =20 return 0; } --=20 2.19.2 From nobody Mon May 6 10:46:36 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) client-ip=207.211.31.81; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from us-smtp-delivery-1.mimecast.com (us-smtp-1.mimecast.com [207.211.31.81]) by mx.zohomail.com with SMTPS id 1580673246780295.8610066293595; Sun, 2 Feb 2020 11:54:06 -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-169-wxpcV9NTNC6_ziA5EQHt1w-1; Sun, 02 Feb 2020 14:54:03 -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 29CB118FE864; Sun, 2 Feb 2020 19:53:58 +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 0105F19C7F; Sun, 2 Feb 2020 19:53: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 BA4F518089D5; Sun, 2 Feb 2020 19:53:57 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 012JrJWs003226 for ; Sun, 2 Feb 2020 14:53:19 -0500 Received: by smtp.corp.redhat.com (Postfix) id 8C33187B1D; Sun, 2 Feb 2020 19:53:19 +0000 (UTC) Received: from icr.redhat.com (unknown [10.43.2.100]) by smtp.corp.redhat.com (Postfix) with ESMTP id E29E286C4A for ; Sun, 2 Feb 2020 19:53:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1580673245; 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=RQwgWbC/hcliebqCvgqmmRbJM/ExYreUw5Vh62ZBJUs=; b=Xs9LJc9v/HJ3VQMBya5GwTqaA+FBit4TZftXqRbiQNL20smHJtkW4263PUhAKJzyiCArg2 4Ij3Vo/X0vzHhhWrdiD4JaWCaCV735+KLuW+hnejVRG2tL1Du8zR3eYsuylbg9+Wr55Aoo SpuOa2K8dev6fNq0aTkv+mH4UPl4XjQ= From: =?UTF-8?q?J=C3=A1n=20Tomko?= To: libvir-list@redhat.com Subject: [libvirt PATCH 3/3] virBufferTrim: do not accept len Date: Sun, 2 Feb 2020 20:53:04 +0100 Message-Id: <82aefa8d507d8f9375933efea9d911dd7a8586dd.1580673055.git.jtomko@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 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 X-MC-Unique: wxpcV9NTNC6_ziA5EQHt1w-1 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" Always trim the full specified suffix. All of the callers outside of tests were passing either strlen or the actual length of the string. Signed-off-by: J=C3=A1n Tomko Reviewed-by: Peter Krempa --- src/conf/domain_addr.c | 2 +- src/conf/domain_conf.c | 6 +++--- src/cpu/cpu_x86.c | 6 +++--- src/qemu/qemu_command.c | 8 ++++---- src/rpc/virnetsshsession.c | 2 +- src/storage/storage_util.c | 4 ++-- src/util/virbuffer.c | 28 ++++++++-------------------- src/util/virbuffer.h | 2 +- src/util/virqemu.c | 2 +- src/util/virresctrl.c | 4 ++-- tests/qemublocktest.c | 2 +- tests/qemumonitorjsontest.c | 4 ++-- tests/virbuftest.c | 13 ++++++------- tools/virsh-domain.c | 8 ++++---- tools/vsh.c | 4 ++-- 15 files changed, 41 insertions(+), 54 deletions(-) diff --git a/src/conf/domain_addr.c b/src/conf/domain_addr.c index bde0784189..c0e468122a 100644 --- a/src/conf/domain_addr.c +++ b/src/conf/domain_addr.c @@ -1946,7 +1946,7 @@ virDomainUSBAddressPortFormatBuf(virBufferPtr buf, break; virBufferAsprintf(buf, "%u.", port[i]); } - virBufferTrim(buf, ".", -1); + virBufferTrim(buf, "."); } =20 =20 diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 9b60db7ecd..c95bd34fb5 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -25521,7 +25521,7 @@ virDomainVirtioNetGuestOptsFormat(char **outstr, virBufferAsprintf(&buf, "ufo=3D'%s' ", virTristateSwitchTypeToString(def->driver.virtio= .guest.ufo)); } - virBufferTrim(&buf, " ", -1); + virBufferTrim(&buf, " "); =20 *outstr =3D virBufferContentAndReset(&buf); return 0; @@ -25561,7 +25561,7 @@ virDomainVirtioNetHostOptsFormat(char **outstr, virBufferAsprintf(&buf, "mrg_rxbuf=3D'%s' ", virTristateSwitchTypeToString(def->driver.virtio= .host.mrg_rxbuf)); } - virBufferTrim(&buf, " ", -1); + virBufferTrim(&buf, " "); =20 *outstr =3D virBufferContentAndReset(&buf); return 0; @@ -30580,7 +30580,7 @@ virDomainGetBlkioParametersAssignFromDef(virDomainD= efPtr def, def->blkio.devices[i].path, \ def->blkio.devices[i].param); \ } \ - virBufferTrim(&buf, ",", -1); \ + virBufferTrim(&buf, ","); \ data =3D virBufferContentAndReset(&buf); \ if (virTypedParameterAssign(&(params[(*nparams)++]), name, \ VIR_TYPED_PARAM_STRING, data) < 0) \ diff --git a/src/cpu/cpu_x86.c b/src/cpu/cpu_x86.c index 9b7981d574..dca9ed2979 100644 --- a/src/cpu/cpu_x86.c +++ b/src/cpu/cpu_x86.c @@ -1981,7 +1981,7 @@ x86FormatSignatures(virCPUx86ModelPtr model) (unsigned long)model->signatures[i]); } =20 - virBufferTrim(&buf, ",", -1); + virBufferTrim(&buf, ","); =20 return virBufferContentAndReset(&buf); } @@ -3059,8 +3059,8 @@ virCPUx86UpdateLive(virCPUDefPtr cpu, } } =20 - virBufferTrim(&bufAdded, ",", -1); - virBufferTrim(&bufRemoved, ",", -1); + virBufferTrim(&bufAdded, ","); + virBufferTrim(&bufRemoved, ","); =20 added =3D virBufferContentAndReset(&bufAdded); removed =3D virBufferContentAndReset(&bufRemoved); diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index c8195cfbb9..81f5fa3416 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -2360,7 +2360,7 @@ qemuBuildFloppyCommandLineControllerOptions(virComman= dPtr cmd, =20 if (explicitfdc && hasfloppy) { /* Newer Q35 machine types require an explicit FDC controller */ - virBufferTrim(&fdc_opts, ",", -1); + virBufferTrim(&fdc_opts, ","); virCommandAddArg(cmd, "-device"); virCommandAddArgBuffer(cmd, &fdc_opts); } @@ -3979,7 +3979,7 @@ qemuBuildHostNetStr(virDomainNetDefPtr net, } =20 =20 - virBufferTrim(&buf, ",", -1); + virBufferTrim(&buf, ","); =20 return virBufferContentAndReset(&buf); } @@ -6330,7 +6330,7 @@ qemuBuildBootCommandLine(virCommandPtr cmd, if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_BOOT_STRICT)) virBufferAddLit(&boot_buf, "strict=3Don,"); =20 - virBufferTrim(&boot_buf, ",", -1); + virBufferTrim(&boot_buf, ","); =20 boot_opts_str =3D virBufferContentAndReset(&boot_buf); if (boot_opts_str) { @@ -7744,7 +7744,7 @@ qemuBuildGraphicsSPICECommandLine(virQEMUDriverConfig= Ptr cfg, * doesn't support it, it fallbacks to previous migration algorithm si= lently. */ virBufferAddLit(&opt, "seamless-migration=3Don,"); =20 - virBufferTrim(&opt, ",", -1); + virBufferTrim(&opt, ","); =20 virCommandAddArg(cmd, "-spice"); virCommandAddArgBuffer(cmd, &opt); diff --git a/src/rpc/virnetsshsession.c b/src/rpc/virnetsshsession.c index 63773ecfc2..c31d80d4eb 100644 --- a/src/rpc/virnetsshsession.c +++ b/src/rpc/virnetsshsession.c @@ -357,7 +357,7 @@ virNetSSHCheckHostKey(virNetSSHSessionPtr sess) * we have to use a *MAGIC* constant. */ for (i =3D 0; i < 16; i++) virBufferAsprintf(&buff, "%02hhX:", keyhash[i]); - virBufferTrim(&buff, ":", 1); + virBufferTrim(&buff, ":"); =20 keyhashstr =3D virBufferContentAndReset(&buff); =20 diff --git a/src/storage/storage_util.c b/src/storage/storage_util.c index d285e66879..7688ea9fd9 100644 --- a/src/storage/storage_util.c +++ b/src/storage/storage_util.c @@ -755,7 +755,7 @@ storageBackendCreateQemuImgOpts(virStorageEncryptionInf= oDefPtr encinfo, } } =20 - virBufferTrim(&buf, ",", -1); + virBufferTrim(&buf, ","); =20 *opts =3D virBufferContentAndReset(&buf); return 0; @@ -4083,7 +4083,7 @@ virStorageBackendFileSystemMountAddOptions(virCommand= Ptr cmd, "mount_opts from XML", def->name, uuidstr); } =20 - virBufferTrim(&buf, ",", -1); + virBufferTrim(&buf, ","); mountOpts =3D virBufferContentAndReset(&buf); =20 if (mountOpts) diff --git a/src/util/virbuffer.c b/src/util/virbuffer.c index b76d99b56f..b78c3debe9 100644 --- a/src/util/virbuffer.c +++ b/src/util/virbuffer.c @@ -637,39 +637,27 @@ virBufferStrcat(virBufferPtr buf, ...) /** * virBufferTrim: * @buf: the buffer to trim - * @str: the optional string, to force an exact trim - * @len: the number of bytes to trim, or -1 to use @str + * @str: the string to be trimmed from the tail * - * Trim the tail of a buffer. If @str is provided, the trim only occurs - * if the current tail of the buffer matches @str; a non-negative @len - * further limits how much of the tail is trimmed. If @str is NULL, then - * @len must be non-negative. + * Trim the supplied string from the tail of the buffer. */ void -virBufferTrim(virBufferPtr buf, const char *str, int len) +virBufferTrim(virBufferPtr buf, const char *str) { - size_t len2 =3D 0; + size_t len =3D 0; =20 if (!buf || !buf->str) return; =20 - if (!str && len < 0) + if (!str) return; =20 + len =3D strlen(str); =20 - if (len > 0 && len > buf->str->len) + if (len > buf->str->len || + memcmp(&buf->str->str[buf->str->len - len], str, len) !=3D 0) return; =20 - if (str) { - len2 =3D strlen(str); - if (len2 > buf->str->len || - memcmp(&buf->str->str[buf->str->len - len2], str, len2) !=3D 0) - return; - } - - if (len < 0) - len =3D len2; - g_string_truncate(buf->str, buf->str->len - len); } =20 diff --git a/src/util/virbuffer.h b/src/util/virbuffer.h index 7b068075b2..b66b1f2b23 100644 --- a/src/util/virbuffer.h +++ b/src/util/virbuffer.h @@ -91,7 +91,7 @@ void virBufferSetIndent(virBufferPtr, int indent); size_t virBufferGetIndent(const virBuffer *buf); size_t virBufferGetEffectiveIndent(const virBuffer *buf); =20 -void virBufferTrim(virBufferPtr buf, const char *trim, int len); +void virBufferTrim(virBufferPtr buf, const char *trim); void virBufferTrimChars(virBufferPtr buf, const char *trim); void virBufferTrimLen(virBufferPtr buf, int len); void virBufferAddStr(virBufferPtr buf, const char *str); diff --git a/src/util/virqemu.c b/src/util/virqemu.c index 8d0429625d..78a9e0480b 100644 --- a/src/util/virqemu.c +++ b/src/util/virqemu.c @@ -220,7 +220,7 @@ virQEMUBuildCommandLineJSON(virJSONValuePtr value, if (virQEMUBuildCommandLineJSONRecurse(NULL, value, buf, array, false)= < 0) return -1; =20 - virBufferTrim(buf, ",", -1); + virBufferTrim(buf, ","); =20 return 0; } diff --git a/src/util/virresctrl.c b/src/util/virresctrl.c index 213d7492d3..569a9ee770 100644 --- a/src/util/virresctrl.c +++ b/src/util/virresctrl.c @@ -1432,7 +1432,7 @@ virResctrlAllocMemoryBandwidthFormat(virResctrlAllocP= tr alloc, } } =20 - virBufferTrim(buf, ";", 1); + virBufferTrim(buf, ";"); virBufferAddChar(buf, '\n'); return 0; } @@ -1574,7 +1574,7 @@ virResctrlAllocFormatCache(virResctrlAllocPtr alloc, VIR_FREE(mask_str); } =20 - virBufferTrim(buf, ";", 1); + virBufferTrim(buf, ";"); virBufferAddChar(buf, '\n'); } } diff --git a/tests/qemublocktest.c b/tests/qemublocktest.c index 3076dc9645..4cfabf0766 100644 --- a/tests/qemublocktest.c +++ b/tests/qemublocktest.c @@ -454,7 +454,7 @@ testQemuImageCreate(const void *opaque) =20 virBufferStrcat(&actualbuf, "protocol:\n", NULLSTR(jsonprotocol), "\nformat:\n", NULLSTR(jsonformat), NULL); - virBufferTrim(&actualbuf, "\n", -1); + virBufferTrim(&actualbuf, "\n"); virBufferAddLit(&actualbuf, "\n"); =20 jsonpath =3D g_strdup_printf("%s%s.json", testQemuImageCreatePath, dat= a->name); diff --git a/tests/qemumonitorjsontest.c b/tests/qemumonitorjsontest.c index 0334f83628..1c7083ca79 100644 --- a/tests/qemumonitorjsontest.c +++ b/tests/qemumonitorjsontest.c @@ -2777,7 +2777,7 @@ testBlockNodeNameDetect(const void *opaque) =20 virHashForEach(nodedata, testBlockNodeNameDetectFormat, &buf); =20 - virBufferTrim(&buf, "\n", -1); + virBufferTrim(&buf, "\n"); =20 actual =3D virBufferContentAndReset(&buf); =20 @@ -2919,7 +2919,7 @@ testQueryJobs(const void *opaque) for (i =3D 0; i < njobs; i++) testQueryJobsPrintJob(&buf, jobs[i]); =20 - virBufferTrim(&buf, "\n", -1); + virBufferTrim(&buf, "\n"); =20 actual =3D virBufferContentAndReset(&buf); =20 diff --git a/tests/virbuftest.c b/tests/virbuftest.c index f2d4d9b9de..f9d19ff1a1 100644 --- a/tests/virbuftest.c +++ b/tests/virbuftest.c @@ -102,21 +102,20 @@ static int testBufTrim(const void *data G_GNUC_UNUSED) const char *expected =3D "a,b"; int ret =3D -1; =20 - virBufferTrim(buf, "", 0); + virBufferTrim(buf, ""); buf =3D &bufinit; =20 virBufferAddLit(buf, "a;"); - virBufferTrim(buf, "", 0); - virBufferTrim(buf, "", -1); + virBufferTrim(buf, ""); + virBufferTrim(buf, ""); virBufferTrimLen(buf, 1); virBufferTrimLen(buf, 5); virBufferTrimLen(buf, 2); =20 virBufferAddLit(buf, ",b,,"); - virBufferTrim(buf, NULL, -1); - virBufferTrim(buf, "b", -1); - virBufferTrim(buf, "b,,", 1); - virBufferTrim(buf, ",", -1); + virBufferTrim(buf, NULL); + virBufferTrim(buf, "b"); + virBufferTrim(buf, ",,"); =20 result =3D virBufferContentAndReset(buf); if (!result || STRNEQ(result, expected)) { diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 0bb5de540e..32b2792694 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -8453,7 +8453,7 @@ cmdDesc(vshControl *ctl, const vshCmd *cmd) while ((opt =3D vshCommandOptArgv(ctl, cmd, opt))) virBufferAsprintf(&buf, "%s ", opt->data); =20 - virBufferTrim(&buf, " ", -1); + virBufferTrim(&buf, " "); =20 desc =3D virBufferContentAndReset(&buf); =20 @@ -9570,7 +9570,7 @@ cmdQemuMonitorCommand(vshControl *ctl, const vshCmd *= cmd) while ((opt =3D vshCommandOptArgv(ctl, cmd, opt))) virBufferAsprintf(&buf, "%s ", opt->data); =20 - virBufferTrim(&buf, " ", -1); + virBufferTrim(&buf, " "); =20 monitor_cmd =3D virBufferContentAndReset(&buf); =20 @@ -9863,7 +9863,7 @@ cmdQemuAgentCommand(vshControl *ctl, const vshCmd *cm= d) while ((opt =3D vshCommandOptArgv(ctl, cmd, opt))) virBufferAsprintf(&buf, "%s ", opt->data); =20 - virBufferTrim(&buf, " ", -1); + virBufferTrim(&buf, " "); =20 guest_agent_cmd =3D virBufferContentAndReset(&buf); =20 @@ -14077,7 +14077,7 @@ cmdDomFSInfo(vshControl *ctl, const vshCmd *cmd) =20 for (j =3D 0; j < info[i]->ndevAlias; j++) virBufferAsprintf(&targetsBuff, "%s,", info[i]->devAlias[j= ]); - virBufferTrim(&targetsBuff, ",", -1); + virBufferTrim(&targetsBuff, ","); =20 targets =3D virBufferContentAndReset(&targetsBuff); =20 diff --git a/tools/vsh.c b/tools/vsh.c index 3c3b4bf1c3..02a6b6c955 100644 --- a/tools/vsh.c +++ b/tools/vsh.c @@ -2225,7 +2225,7 @@ vshOutputLogFile(vshControl *ctl, int log_level, cons= t char *msg_format, } virBufferAsprintf(&buf, "%s ", lvl); virBufferVasprintf(&buf, msg_format, ap); - virBufferTrim(&buf, "\n", -1); + virBufferTrim(&buf, "\n"); virBufferAddChar(&buf, '\n'); =20 str =3D virBufferContentAndReset(&buf); @@ -2517,7 +2517,7 @@ vshTreePrintInternal(vshControl *ctl, false, indent) < 0) return -1; } - virBufferTrim(indent, " ", -1); + virBufferTrim(indent, " "); =20 /* If there was no child device, and we're the last in * a list of devices, then print another blank line */ --=20 2.19.2