* Re: [PATCH] qga: fix a memory leak in qmp_guest_exec_status()
2021-06-30 15:33 [PATCH] qga: fix a memory leak in qmp_guest_exec_status() Yi Wang
@ 2021-06-30 7:49 ` Marc-André Lureau
0 siblings, 0 replies; 2+ messages in thread
From: Marc-André Lureau @ 2021-06-30 7:49 UTC (permalink / raw)
To: Yi Wang; +Cc: xue.zhihong, Wang Yechao, QEMU, wang.liang82
[-- Attachment #1: Type: text/plain, Size: 1883 bytes --]
Hi
On Wed, Jun 30, 2021 at 11:42 AM Yi Wang <wang.yi59@zte.com.cn> wrote:
> From: Wang Yechao <wang.yechao255@zte.com.cn>
>
> The $GuestExecInfo.out.data is alloced in guest_exec_output_watch(),
> and the buffer size is $GuestExecInfo.out.size. We should free the
> $GuestExecInfo.out.data judge by the size, not length. Because the
> $GuestExecInfo.out.length maybe zero in some case.
>
> $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>
> ---
> qga/commands.c | 8 ++++++--
> 1 file changed, 6 insertions(+), 2 deletions(-)
>
> diff --git a/qga/commands.c b/qga/commands.c
> index d3fec80..58d4da9 100644
> --- a/qga/commands.c
> +++ b/qga/commands.c
>
> @@ -214,17 +214,21 @@ 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;
> }
>
> + if (gei->out.size > 0)
> + g_free(gei->out.data);
> +
> + if (gei->err.size > 0)
> + g_free(gei->err.data);
>
You shouldn't need to check if size > 0. You can just call g_free() on the
fields (NULL is ignored).
Looks good otherwise
+
> QTAILQ_REMOVE(&guest_exec_state.processes, gei, next);
> g_free(gei);
> }
> --
> 1.8.3.1
>
--
Marc-André Lureau
[-- Attachment #2: Type: text/html, Size: 3041 bytes --]
^ permalink raw reply [flat|nested] 2+ messages in thread
* [PATCH] qga: fix a memory leak in qmp_guest_exec_status()
@ 2021-06-30 15:33 Yi Wang
2021-06-30 7:49 ` Marc-André Lureau
0 siblings, 1 reply; 2+ messages in thread
From: Yi Wang @ 2021-06-30 15:33 UTC (permalink / raw)
To: qemu-devel; +Cc: wang.yi59, Wang Yechao, wang.liang82, xue.zhihong
[-- Attachment #1.1.1: Type: text/plain, Size: 1547 bytes --]
From: Wang Yechao <wang.yechao255@zte.com.cn>
The $GuestExecInfo.out.data is alloced in guest_exec_output_watch(),
and the buffer size is $GuestExecInfo.out.size. We should free the
$GuestExecInfo.out.data judge by the size, not length. Because the
$GuestExecInfo.out.length maybe zero in some case.
$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>
---
qga/commands.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/qga/commands.c b/qga/commands.c
index d3fec80..58d4da9 100644
--- a/qga/commands.c
+++ b/qga/commands.c
@@ -214,17 +214,21 @@ 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;
}
+ if (gei->out.size > 0)
+ g_free(gei->out.data);
+
+ if (gei->err.size > 0)
+ 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: 3338 bytes --]
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2021-06-30 7:50 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-30 15:33 [PATCH] qga: fix a memory leak in qmp_guest_exec_status() Yi Wang
2021-06-30 7:49 ` Marc-André Lureau
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).