diff mbox series

[3/4] libqtest: fix memory leak in the qtest_qmp_event_ref

Message ID 20201019163702.471239-4-mlevitsk@redhat.com (mailing list archive)
State New, archived
Headers show
Series Assorted fixes to tests that were broken by recent scsi changes | expand

Commit Message

Maxim Levitsky Oct. 19, 2020, 4:37 p.m. UTC
The g_list_remove_link doesn't free the link element,
opposed to what I thought.
Switch to g_list_delete_link that does free it.

Also refactor the code a bit.
Thanks for Max Reitz for helping me with this.

Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com>
---
 tests/qtest/libqtest.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

Comments

Thomas Huth Oct. 24, 2020, 5:37 a.m. UTC | #1
On 19/10/2020 18.37, Maxim Levitsky wrote:
> The g_list_remove_link doesn't free the link element,
> opposed to what I thought.
> Switch to g_list_delete_link that does free it.
> 
> Also refactor the code a bit.
> Thanks for Max Reitz for helping me with this.
> 
> Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com>
> ---
>  tests/qtest/libqtest.c | 11 ++++-------
>  1 file changed, 4 insertions(+), 7 deletions(-)
> 
> diff --git a/tests/qtest/libqtest.c b/tests/qtest/libqtest.c
> index bd96cb6fdd..9ae052d566 100644
> --- a/tests/qtest/libqtest.c
> +++ b/tests/qtest/libqtest.c
> @@ -795,15 +795,12 @@ void qtest_qmp_send_raw(QTestState *s, const char *fmt, ...)
>  
>  QDict *qtest_qmp_event_ref(QTestState *s, const char *event)
>  {
> -    GList *next = NULL;
> -    QDict *response;
> -
> -    for (GList *it = s->pending_events; it != NULL; it = next) {
> +    while (s->pending_events) {
>  
> -        next = it->next;
> -        response = (QDict *)it->data;
> +        GList *first = s->pending_events;
> +        QDict *response = (QDict *)first->data;
>  
> -        s->pending_events = g_list_remove_link(s->pending_events, it);
> +        s->pending_events = g_list_delete_link(s->pending_events, first);
>  
>          if (!strcmp(qdict_get_str(response, "event"), event)) {
>              return response;
> 

Thanks, queued (together with patch 2) to qtest-next:

 https://gitlab.com/huth/qemu/-/commits/qtest-next/

 Thomas
diff mbox series

Patch

diff --git a/tests/qtest/libqtest.c b/tests/qtest/libqtest.c
index bd96cb6fdd..9ae052d566 100644
--- a/tests/qtest/libqtest.c
+++ b/tests/qtest/libqtest.c
@@ -795,15 +795,12 @@  void qtest_qmp_send_raw(QTestState *s, const char *fmt, ...)
 
 QDict *qtest_qmp_event_ref(QTestState *s, const char *event)
 {
-    GList *next = NULL;
-    QDict *response;
-
-    for (GList *it = s->pending_events; it != NULL; it = next) {
+    while (s->pending_events) {
 
-        next = it->next;
-        response = (QDict *)it->data;
+        GList *first = s->pending_events;
+        QDict *response = (QDict *)first->data;
 
-        s->pending_events = g_list_remove_link(s->pending_events, it);
+        s->pending_events = g_list_delete_link(s->pending_events, first);
 
         if (!strcmp(qdict_get_str(response, "event"), event)) {
             return response;