From nobody Mon Feb 9 03:52:04 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=1623422977; cv=none; d=zohomail.com; s=zohoarc; b=Cne956EELRfdcJp/ofoLGQRUBnt/dne4aRlKZiELjnDVYieZvT7FeL9GcbGgIxlCQ4/gmYTGyfmWAeKaxeOdeOp6XROhmJ4RIx00MrN7XRz0MCTJ4ZpdQ81i0qbnQbw4YQihJcq0BSwVE7AEAP+EQ5qqpGSbfaOF0GThA49oTXk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1623422977; 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=VlTmZeX2KkiLCyjzMtmkb/EPOeIvYewHo/M/XXILbPg=; b=c29bsAP02/6Wv7gz0CNf39WqFO32JamApl/CLw1EEoLD4ZysBotuci768JiRHBqk/dUw9fSt6cGl53CQDidPfQOEkFQm7+ztGU3T8AMN6hvHf4qpWW2VCX+qf8bE2+pd72axLs73Xm9GAKHlz1x16uuzy5WFDDtIlGfKa6Pq/4Y= 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 1623422977910805.2766210849503; Fri, 11 Jun 2021 07:49:37 -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-458-KD2tQqYiNU-f6I9JxcLYJw-1; Fri, 11 Jun 2021 10:49:35 -0400 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 5FE381097032; Fri, 11 Jun 2021 14:49:27 +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 3361719D9B; Fri, 11 Jun 2021 14:49: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 CA30B46F82; Fri, 11 Jun 2021 14:49:26 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 15BEnGP9007595 for ; Fri, 11 Jun 2021 10:49:16 -0400 Received: by smtp.corp.redhat.com (Postfix) id 62C5560FC2; Fri, 11 Jun 2021 14:49:16 +0000 (UTC) Received: from speedmetal.redhat.com (unknown [10.40.208.33]) by smtp.corp.redhat.com (Postfix) with ESMTP id BCF1360C05 for ; Fri, 11 Jun 2021 14:49:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1623422976; 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=VlTmZeX2KkiLCyjzMtmkb/EPOeIvYewHo/M/XXILbPg=; b=Sqy01fU8NCN9qER7YeH9v63GPWibpt2CHme8OEFosjZ7DrP9uvCGakYuwWUd3HNuXghn5Z qW+uHYA4wO3+nRCZGwGooYuak9yrma+KhZIor9BC4fQvYnY3x3NCrwDpwNPwMzr9iM4/66 TSMBuZbZm1M+3lxjuwX5n3W/L1uaDLY= X-MC-Unique: KD2tQqYiNU-f6I9JxcLYJw-1 From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 01/22] util: command: Introduce virCommandToStringBuf Date: Fri, 11 Jun 2021 16:48:52 +0200 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 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) Content-Type: text/plain; charset="utf-8" The new version allows passing a virBuffer to format the string into. This will be helpful in solving a memory lean in wrong usage of virCommandToString and also in tests where we need to add a newline after the command in certain cases. Signed-off-by: Peter Krempa --- src/libvirt_private.syms | 1 + src/util/vircommand.c | 50 +++++++++++++++++++++++++--------------- src/util/vircommand.h | 4 ++++ 3 files changed, 37 insertions(+), 18 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 0ced2a7990..9700e14def 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -2037,6 +2037,7 @@ virCommandSetUID; virCommandSetUmask; virCommandSetWorkingDirectory; virCommandToString; +virCommandToStringBuf; virCommandToStringFull; virCommandWait; virCommandWriteArgLog; diff --git a/src/util/vircommand.c b/src/util/vircommand.c index 6e01e4cb62..8e372c3152 100644 --- a/src/util/vircommand.c +++ b/src/util/vircommand.c @@ -2053,8 +2053,9 @@ virCommandWriteArgLog(virCommand *cmd, int logfd) /** - * virCommandToStringFull: + * virCommandToStringBuf: * @cmd: the command to convert + * @buf: buffer to format @cmd into * @linebreaks: true to break line after each env var or option * @stripCommandPath: strip the path leading to the binary of @cmd * @@ -2062,16 +2063,15 @@ virCommandWriteArgLog(virCommand *cmd, int logfd) * before Run/RunAsync, to return a string representation of the * environment and arguments of cmd, suitably quoted for pasting into * a shell. If virCommandRun cannot succeed (because of an - * out-of-memory condition while building cmd), NULL will be returned. - * Caller is responsible for freeing the resulting string. + * out-of-memory condition while building cmd), -1 will be returned. */ -char * -virCommandToStringFull(virCommand *cmd, - bool linebreaks, - bool stripCommandPath) +int +virCommandToStringBuf(virCommand *cmd, + virBuffer *buf, + bool linebreaks, + bool stripCommandPath) { size_t i; - g_auto(virBuffer) buf =3D VIR_BUFFER_INITIALIZER; const char *command =3D cmd->args[0]; g_autofree char *basename =3D NULL; bool had_option =3D false; @@ -2080,7 +2080,7 @@ virCommandToStringFull(virCommand *cmd, * now. If virCommandRun is called, it will report the same error. */ if (virCommandHasError(cmd)) { virCommandRaiseError(cmd); - return NULL; + return -1; } for (i =3D 0; i < cmd->nenv; i++) { @@ -2091,22 +2091,22 @@ virCommandToStringFull(virCommand *cmd, if (!eq) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("invalid use of command API")); - return NULL; + return -1; } eq++; - virBufferAdd(&buf, cmd->env[i], eq - cmd->env[i]); - virBufferEscapeShell(&buf, eq); - virBufferAddChar(&buf, ' '); + virBufferAdd(buf, cmd->env[i], eq - cmd->env[i]); + virBufferEscapeShell(buf, eq); + virBufferAddChar(buf, ' '); if (linebreaks) - virBufferAddLit(&buf, "\\\n"); + virBufferAddLit(buf, "\\\n"); } if (stripCommandPath) command =3D basename =3D g_path_get_basename(command); - virBufferEscapeShell(&buf, command); + virBufferEscapeShell(buf, command); for (i =3D 1; i < cmd->nargs; i++) { - virBufferAddChar(&buf, ' '); + virBufferAddChar(buf, ' '); if (linebreaks) { /* we don't want a linebreak only if @@ -2131,11 +2131,25 @@ virCommandToStringFull(virCommand *cmd, } if (linebreak) - virBufferAddLit(&buf, "\\\n"); + virBufferAddLit(buf, "\\\n"); } - virBufferEscapeShell(&buf, cmd->args[i]); + virBufferEscapeShell(buf, cmd->args[i]); } + return 0; +} + + +char * +virCommandToStringFull(virCommand *cmd, + bool linebreaks, + bool stripCommandPath) +{ + g_auto(virBuffer) buf =3D VIR_BUFFER_INITIALIZER; + + if (virCommandToStringBuf(cmd, &buf, linebreaks, stripCommandPath)) + return NULL; + return virBufferContentAndReset(&buf); } diff --git a/src/util/vircommand.h b/src/util/vircommand.h index 77cf8c8ec9..fb6c3b5d12 100644 --- a/src/util/vircommand.h +++ b/src/util/vircommand.h @@ -173,6 +173,10 @@ char *virCommandToString(virCommand *cmd, bool linebre= aks) G_GNUC_WARN_UNUSED_RE char *virCommandToStringFull(virCommand *cmd, bool linebreaks, bool stripCommandPath); +int virCommandToStringBuf(virCommand *cmd, + virBuffer *buf, + bool linebreaks, + bool stripCommandPath); int virCommandGetArgList(virCommand *cmd, char ***args, size_t *nargs); --=20 2.31.1