[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH 2/2] libvhost-user: make vu_queue_empty() safer
From: |
Marc-André Lureau |
Subject: |
[Qemu-devel] [PATCH 2/2] libvhost-user: make vu_queue_empty() safer |
Date: |
Wed, 3 May 2017 16:11:43 +0400 |
Check if vring.avail != null before accessing it.
Fix crashes from callers such a vu_queue_pop() when queue isn't ready.
Fix documentation and return type while at it.
Signed-off-by: Marc-André Lureau <address@hidden>
---
contrib/libvhost-user/libvhost-user.h | 6 +++---
contrib/libvhost-user/libvhost-user.c | 8 ++++++--
2 files changed, 9 insertions(+), 5 deletions(-)
diff --git a/contrib/libvhost-user/libvhost-user.h
b/contrib/libvhost-user/libvhost-user.h
index 156b50e989..af02a31ebe 100644
--- a/contrib/libvhost-user/libvhost-user.h
+++ b/contrib/libvhost-user/libvhost-user.h
@@ -327,13 +327,13 @@ void vu_queue_set_notification(VuDev *dev, VuVirtq *vq,
int enable);
bool vu_queue_enabled(VuDev *dev, VuVirtq *vq);
/**
- * vu_queue_enabled:
+ * vu_queue_empty:
* @dev: a VuDev context
* @vq: a VuVirtq queue
*
- * Returns: whether the queue is empty.
+ * Returns: true if the queue is empty or not ready.
*/
-int vu_queue_empty(VuDev *dev, VuVirtq *vq);
+bool vu_queue_empty(VuDev *dev, VuVirtq *vq);
/**
* vu_queue_notify:
diff --git a/contrib/libvhost-user/libvhost-user.c
b/contrib/libvhost-user/libvhost-user.c
index f9680b6279..b0f15b76e3 100644
--- a/contrib/libvhost-user/libvhost-user.c
+++ b/contrib/libvhost-user/libvhost-user.c
@@ -1125,11 +1125,15 @@ vu_queue_avail_bytes(VuDev *dev, VuVirtq *vq, unsigned
int in_bytes,
/* Fetch avail_idx from VQ memory only when we really need to know if
* guest has added some buffers. */
-int
+bool
vu_queue_empty(VuDev *dev, VuVirtq *vq)
{
+ if (!vq->vring.avail) {
+ return true;
+ }
+
if (vq->shadow_avail_idx != vq->last_avail_idx) {
- return 0;
+ return false;
}
return vring_avail_idx(vq) == vq->last_avail_idx;
--
2.12.0.191.gc5d8de91d