]> www.infradead.org Git - users/dwmw2/qemu.git/commitdiff
libqtest: fix memory leak in the qtest_qmp_event_ref
authorMaxim Levitsky <mlevitsk@redhat.com>
Mon, 19 Oct 2020 16:37:01 +0000 (19:37 +0300)
committerThomas Huth <thuth@redhat.com>
Sat, 24 Oct 2020 05:36:53 +0000 (07:36 +0200)
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>
Message-Id: <20201019163702.471239-4-mlevitsk@redhat.com>
Signed-off-by: Thomas Huth <thuth@redhat.com>
tests/qtest/libqtest.c

index 0e304bdbd1512a3d66a7a4f8a67e371ace851f77..99deff47efcbb4e56fe8564453b1063305ddd222 100644 (file)
@@ -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;