From nobody Mon Feb 9 17:34:49 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) client-ip=170.10.133.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 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=1644601716; cv=none; d=zohomail.com; s=zohoarc; b=W+YIOSxEJidmCE0yPMy+BUJMTO1P9IIZZATEEz5LZnh+VwdTWft58w/lx9XOWXkkZ+/lzk9T3rkmQAMASGK4SOCPB5ZfJaonrAKQdxawAO6VZJPc6rkgb3NUmn/qa3AOZe+JLfnD7nsFsn+xDE/RpEvshoyAb3ULSRTshkKzyF0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1644601716; 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=BKmq0bYmku7QPQhkxwVxZWAUTBciXFQVCPV3yhEk/Wo=; b=evNddUuC6Lt/wuRncVUuLTidGQXXWO+OvAyrWpskrzz35sT/6fiYiX9xs9rDqnSDHcws62sLwUw3DTn6WqpSdjIGe+4AEj/IOqD5iMUVGxDRUmN0N80Dlrd6Q08D2Van55/aqHOiXsNVLh92YfQMwBVIU5s908eC7XxMUXe40Xk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 170.10.133.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by mx.zohomail.com with SMTPS id 1644601716327258.95886826213564; Fri, 11 Feb 2022 09:48:36 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-326--0kJpByIPTyK3De7KANL_w-1; Fri, 11 Feb 2022 12:48:33 -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 3F0181091DA7; Fri, 11 Feb 2022 17:48:28 +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 BC8992DE99; Fri, 11 Feb 2022 17:48:27 +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 79D8D1806D1C; Fri, 11 Feb 2022 17:48:27 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 21BHdc9F006596 for ; Fri, 11 Feb 2022 12:39:38 -0500 Received: by smtp.corp.redhat.com (Postfix) id 9783134D41; Fri, 11 Feb 2022 17:39:38 +0000 (UTC) Received: from harajuku.usersys.redhat.com.homenet.telecomitalia.it (unknown [10.40.194.192]) by smtp.corp.redhat.com (Postfix) with ESMTPS id DF25034D3E for ; Fri, 11 Feb 2022 17:39:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1644601715; 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=BKmq0bYmku7QPQhkxwVxZWAUTBciXFQVCPV3yhEk/Wo=; b=SKHXmOAn0O2EbGrASiJQSPYxIxVCw3T7bBcXjhyWpi0LdJni0B1fCvwMxRzdMevuURlk65 BIvXba5a1I2YmgK4m3Wj2NZ7cOCGHblKpAzpE28ZevXL+Rwl53tu/Y5OurdNDdQt3DeIe9 RBQCO6mtkSCIpfk2/qWc8KvKZYHN+Ws= X-MC-Unique: -0kJpByIPTyK3De7KANL_w-1 From: Andrea Bolognani To: libvir-list@redhat.com Subject: [libvirt PATCH 02/11] virbuffer: Simplify virBufferEscapeShell() Date: Fri, 11 Feb 2022 18:39:10 +0100 Message-Id: <20220211173919.112353-3-abologna@redhat.com> In-Reply-To: <20220211173919.112353-1-abologna@redhat.com> References: <20220211173919.112353-1-abologna@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 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 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) X-ZM-MESSAGEID: 1644601717075100001 Content-Type: text/plain; charset="utf-8" We can exit early when the input is an empty string, and we can avoid storing the string length in a variable since we only use that information once. Signed-off-by: Andrea Bolognani --- src/util/virbuffer.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/util/virbuffer.c b/src/util/virbuffer.c index a4834174a1..19cf775a8c 100644 --- a/src/util/virbuffer.c +++ b/src/util/virbuffer.c @@ -552,7 +552,6 @@ virBufferURIEncodeString(virBuffer *buf, const char *st= r) void virBufferEscapeShell(virBuffer *buf, const char *str) { - int len; g_autofree char *escaped =3D NULL; char *out; const char *cur; @@ -560,21 +559,19 @@ virBufferEscapeShell(virBuffer *buf, const char *str) if ((buf =3D=3D NULL) || (str =3D=3D NULL)) return; =20 - /* Only quote if str includes shell metacharacters. */ - if (*str && !strpbrk(str, "\r\t\n !\"#$&'()*;<>?[\\]^`{|}~")) { - virBufferAdd(buf, str, -1); + if (!*str) { + virBufferAddLit(buf, "''"); return; } =20 - if (*str) { - len =3D strlen(str); - - escaped =3D g_malloc0_n(len + 1, 4); - } else { - virBufferAddLit(buf, "''"); + /* Only quote if str includes shell metacharacters. */ + if (!strpbrk(str, "\r\t\n !\"#$&'()*;<>?[\\]^`{|}~")) { + virBufferAdd(buf, str, -1); return; } =20 + escaped =3D g_malloc0_n(strlen(str) + 1, 4); + cur =3D str; out =3D escaped; =20 --=20 2.34.1