]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
win32: Properly handle TlsGetValue returning NULL
authorMatthias Bolte <matthias.bolte@googlemail.com>
Sat, 21 Apr 2012 17:03:08 +0000 (19:03 +0200)
committerMatthias Bolte <matthias.bolte@googlemail.com>
Sat, 21 Apr 2012 17:03:08 +0000 (19:03 +0200)
virThreadSelf tries to access the virThreadPtr stored in TLS for the
current thread via TlsGetValue. When virThreadSelf is called on a thread
that was not created via virThreadCreate (e.g. the main thread) then
TlsGetValue returns NULL as TlsAlloc initializes TLS slots to NULL.

virThreadSelf can be called on the main thread via this call chain from
virsh

vshDeinit
virEventAddTimeout
virEventPollAddTimeout
virEventPollInterruptLocked
virThreadIsSelf

triggering a segfault as virThreadSelf unconditionally dereferences the
return value of TlsGetValue.

Fix this by making virThreadSelf check the TLS slot value for NULL and
setting the given virThreadPtr accordingly.

Reported by Marcel Müller.

src/util/threads-win32.c

index 157439ceae98912f2da9c7b51848cd139e9c27e4..20756a107fee8941e7abafd5947c08d3af226c11 100644 (file)
@@ -316,8 +316,15 @@ int virThreadCreate(virThreadPtr thread,
 void virThreadSelf(virThreadPtr thread)
 {
     virThreadPtr self = TlsGetValue(selfkey);
-    thread->thread = self->thread;
-    thread->joinable = self->joinable;
+
+    if (self == NULL) {
+        /* called on a thread not created by virThreadCreate, e.g. the main thread */
+        thread->thread = 0;
+        thread->joinable = false;
+    } else {
+        thread->thread = self->thread;
+        thread->joinable = self->joinable;
+    }
 }
 
 bool virThreadIsSelf(virThreadPtr thread)