]> www.infradead.org Git - users/dwmw2/qemu.git/commitdiff
util/main-loop: Avoid adding the same HANDLE twice
authorBin Meng <bin.meng@windriver.com>
Wed, 19 Oct 2022 10:20:14 +0000 (18:20 +0800)
committerPaolo Bonzini <pbonzini@redhat.com>
Sun, 6 Nov 2022 08:48:26 +0000 (09:48 +0100)
Fix the logic in qemu_add_wait_object() to avoid adding the same
HANDLE twice, as the behavior is undefined when passing an array
that contains same HANDLEs to WaitForMultipleObjects() API.

Signed-off-by: Bin Meng <bin.meng@windriver.com>
Message-Id: <20221019102015.2441622-2-bmeng.cn@gmail.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
include/qemu/main-loop.h
util/main-loop.c

index aac707d073a148f92f5ae7e55ac6bca42aabbcd7..3c9a9a982defc866672dbfbbe2cd913877798b73 100644 (file)
@@ -157,6 +157,8 @@ typedef void WaitObjectFunc(void *opaque);
  * in the main loop's calls to WaitForMultipleObjects.  When the handle
  * is in a signaled state, QEMU will call @func.
  *
+ * If the same HANDLE is added twice, this function returns -1.
+ *
  * @handle: The Windows handle to be observed.
  * @func: A function to be called when @handle is in a signaled state.
  * @opaque: A pointer-size value that is passed to @func.
index de38876064e4deaecd2115a15fd730455842b307..10fa74c6e319418247ccf29d53ca7d92f02963c7 100644 (file)
@@ -373,10 +373,20 @@ static WaitObjects wait_objects = {0};
 
 int qemu_add_wait_object(HANDLE handle, WaitObjectFunc *func, void *opaque)
 {
+    int i;
     WaitObjects *w = &wait_objects;
+
     if (w->num >= MAXIMUM_WAIT_OBJECTS) {
         return -1;
     }
+
+    for (i = 0; i < w->num; i++) {
+        /* check if the same handle is added twice */
+        if (w->events[i] == handle) {
+            return -1;
+        }
+    }
+
     w->events[w->num] = handle;
     w->func[w->num] = func;
     w->opaque[w->num] = opaque;