qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Yi Wang <wang.yi59@zte.com.cn>
To: qemu-devel@nongnu.org, marcandre.lureau@gmail.com
Cc: wang.yi59@zte.com.cn, Wang Yechao <wang.yechao255@zte.com.cn>,
	wang.liang82@zte.com.cn, xue.zhihong@zte.com.cn
Subject: [PATCH v2] qga: fix a memory leak in qmp_guest_exec_status()
Date: Thu,  1 Jul 2021 01:44:41 +0800	[thread overview]
Message-ID: <1625075081-2292-1-git-send-email-wang.yi59@zte.com.cn> (raw)


[-- Attachment #1.1.1: Type: text/plain, Size: 1477 bytes --]

From: Wang Yechao <wang.yechao255@zte.com.cn>

In some case, $GuestExecInfo.out.length maybe zero and the memory
is leaked in qmp_guest_exec_status(). Call g_free() on the fileds
directly to fix memory leak (NULL is ignored).

$GuestExecInfo.err.data has the same problem.

Signed-off-by: Yechao Wang <wang.yechao255@zte.com.cn>
Signed-off-by: Yi Wang <wang.yi59@zte.com.cn>

---
Changes in v2:
 - do not check the size > 0 when call g_free. Thanks to Marc.
---
 qga/commands.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/qga/commands.c b/qga/commands.c
index d3fec80..26e9358 100644
--- a/qga/commands.c
+++ b/qga/commands.c
@@ -214,17 +214,18 @@ GuestExecStatus *qmp_guest_exec_status(int64_t pid, Error **errp)
         if (gei->out.length > 0) {
             ges->has_out_data = true;
             ges->out_data = g_base64_encode(gei->out.data, gei->out.length);
-            g_free(gei->out.data);
             ges->has_out_truncated = gei->out.truncated;
         }
 
         if (gei->err.length > 0) {
             ges->has_err_data = true;
             ges->err_data = g_base64_encode(gei->err.data, gei->err.length);
-            g_free(gei->err.data);
             ges->has_err_truncated = gei->err.truncated;
         }
 
+        g_free(gei->out.data);
+        g_free(gei->err.data);
+
         QTAILQ_REMOVE(&guest_exec_state.processes, gei, next);
         g_free(gei);
     }
-- 
1.8.3.1

[-- Attachment #1.1.2: Type: text/html , Size: 3165 bytes --]

             reply	other threads:[~2021-06-30  9:56 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-30 17:44 Yi Wang [this message]
2021-06-30 14:02 ` [PATCH v2] qga: fix a memory leak in qmp_guest_exec_status() Marc-André Lureau

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1625075081-2292-1-git-send-email-wang.yi59@zte.com.cn \
    --to=wang.yi59@zte.com.cn \
    --cc=marcandre.lureau@gmail.com \
    --cc=qemu-devel@nongnu.org \
    --cc=wang.liang82@zte.com.cn \
    --cc=wang.yechao255@zte.com.cn \
    --cc=xue.zhihong@zte.com.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).