vhost: svq: fix uninitialized variable
PATCH 1/1 vhost: svq: fix uninitialized variable
The problem has been reported by gcc with CFLAGS=-O3:
.../hw/virtio/vhost-shadow-virtqueue.c: In function ‘vhost_svq_poll’:.../hw/virtio/vhost-shadow-virtqueue.c:538:12:
error: ‘len’ may be used uninitialized [-Werror=maybe-uninitialized]
538 | return len;
| ^~~
vhost_svq_get_buf() returns NULL if SVQ is empty but doesn't set len to 0, and vhost_svq_poll() returns len without checking the return of vhost_svq_get_buf(). So if the SVQ is empty vhost_svq_poll() can return an random value.
--- hw/virtio/vhost-shadow-virtqueue.c | 1 + 1 file changed, 1 insertion(+)
hw/virtio/vhost-shadow-virtqueue.c +1 -0
@@ -420,6 +420,7 @@ static VirtQueueElement *vhost_svq_get_buf(VhostShadowVirtqueue *svq,vring_used_elem_t used_elem;uint16_t last_used, last_used_chain, num; + *len =0 ;if (!vhost_svq_more_used (svq)) {return NULL ; }
Signature · 1 line
2.39.1
Quoted 9 lines · Laurent Vivier
The problem has been reported by gcc with CFLAGS=-O3:
.../hw/virtio/vhost-shadow-virtqueue.c: In function ‘vhost_svq_poll’:.../hw/virtio/vhost-shadow-virtqueue.c:538:12:
error: ‘len’ may be used uninitialized [-Werror=maybe-uninitialized]
538 | return len;
| ^~~
Quoted 5 lines
vhost_svq_get_buf()returns NULL if SVQ is empty but doesn't set len to 0, andvhost_svq_poll()returns len without checking the return ofvhost_svq_get_buf(). So if the SVQ is emptyvhost_svq_poll()can return an random value.
s/an random/a random/.
I think this solves the same as
https://www.mail-archive.com/[email protected]/msg939383.html ?
Thanks!
Quoted 20 lines
Signed-off-by Laurent Vivier <[email protected]>--- hw/virtio/vhost-shadow-virtqueue.c | 1 + 1 file changed, 1 insertion(+)hw/virtio/vhost-shadow-virtqueue.c +1
-1@@ -420,6 +420,7 @@ static VirtQueueElement *vhost_svq_get_buf(VhostShadowVirtqueue *svq,vring_used_elem_t used_elem;uint16_t last_used, last_used_chain, num; + *len =0 ;if (!vhost_svq_more_used (svq)) {return NULL ; } --2.39.1
Quoted 16 lines · Eugenio Perez Martin
Quoted 9 lines · Laurent Vivier
The problem has been reported by gcc with CFLAGS=-O3:
.../hw/virtio/vhost-shadow-virtqueue.c: In function ‘vhost_svq_poll’:.../hw/virtio/vhost-shadow-virtqueue.c:538:12:
error: ‘len’ may be used uninitialized [-Werror=maybe-uninitialized]
538 | return len;
| ^~~Quoted 5 lines
vhost_svq_get_buf()returns NULL if SVQ is empty but doesn't set len to 0, andvhost_svq_poll()returns len without checking the return ofvhost_svq_get_buf(). So if the SVQ is emptyvhost_svq_poll()can return an random value.
Quoted 4 lines
s/an random/a random/.
I think this solves the same as
https://www.mail-archive.com/[email protected]/msg939383.html ?
Yes, exactly.
Thanks,
Laurent
Quoted 24 lines
Thanks!
Quoted 20 lines
Signed-off-by Laurent Vivier <[email protected]>--- hw/virtio/vhost-shadow-virtqueue.c | 1 + 1 file changed, 1 insertion(+)hw/virtio/vhost-shadow-virtqueue.c +1
-1@@ -420,6 +420,7 @@ static VirtQueueElement *vhost_svq_get_buf(VhostShadowVirtqueue *svq,vring_used_elem_t used_elem;uint16_t last_used, last_used_chain, num; + *len =0 ;if (!vhost_svq_more_used (svq)) {return NULL ; } --2.39.1