qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] vhost: svq: fix uninitialized variable
@ 2023-02-17 10:42 Laurent Vivier
  2023-02-17 11:24 ` Eugenio Perez Martin
  0 siblings, 1 reply; 3+ messages in thread
From: Laurent Vivier @ 2023-02-17 10:42 UTC (permalink / raw)
  To: qemu-devel
  Cc: Eugenio Pérez, Jason Wang, Michael S. Tsirkin,
	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;
      |            ^~~

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.

Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
 hw/virtio/vhost-shadow-virtqueue.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/hw/virtio/vhost-shadow-virtqueue.c b/hw/virtio/vhost-shadow-virtqueue.c
index 430729635815..31cf642db267 100644
--- a/hw/virtio/vhost-shadow-virtqueue.c
+++ b/hw/virtio/vhost-shadow-virtqueue.c
@@ -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



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] vhost: svq: fix uninitialized variable
  2023-02-17 10:42 [PATCH] vhost: svq: fix uninitialized variable Laurent Vivier
@ 2023-02-17 11:24 ` Eugenio Perez Martin
  2023-02-17 11:44   ` Laurent Vivier
  0 siblings, 1 reply; 3+ messages in thread
From: Eugenio Perez Martin @ 2023-02-17 11:24 UTC (permalink / raw)
  To: Laurent Vivier; +Cc: qemu-devel, Jason Wang, Michael S. Tsirkin

On Fri, Feb 17, 2023 at 11:42 AM Laurent Vivier <lvivier@redhat.com> wrote:
>
> 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.
>

s/an random/a random/.

I think this solves the same as
https://www.mail-archive.com/qemu-devel@nongnu.org/msg939383.html ?

Thanks!

> Signed-off-by: Laurent Vivier <lvivier@redhat.com>
> ---
>  hw/virtio/vhost-shadow-virtqueue.c | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/hw/virtio/vhost-shadow-virtqueue.c b/hw/virtio/vhost-shadow-virtqueue.c
> index 430729635815..31cf642db267 100644
> --- a/hw/virtio/vhost-shadow-virtqueue.c
> +++ b/hw/virtio/vhost-shadow-virtqueue.c
> @@ -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
>



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] vhost: svq: fix uninitialized variable
  2023-02-17 11:24 ` Eugenio Perez Martin
@ 2023-02-17 11:44   ` Laurent Vivier
  0 siblings, 0 replies; 3+ messages in thread
From: Laurent Vivier @ 2023-02-17 11:44 UTC (permalink / raw)
  To: Eugenio Perez Martin; +Cc: qemu-devel, Jason Wang, Michael S. Tsirkin

On 2/17/23 12:24, Eugenio Perez Martin wrote:
> On Fri, Feb 17, 2023 at 11:42 AM Laurent Vivier <lvivier@redhat.com> wrote:
>>
>> 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.
>>
> 
> s/an random/a random/.
> 
> I think this solves the same as
> https://www.mail-archive.com/qemu-devel@nongnu.org/msg939383.html ?

Yes, exactly.

Thanks,
Laurent

> 
> Thanks!
> 
>> Signed-off-by: Laurent Vivier <lvivier@redhat.com>
>> ---
>>   hw/virtio/vhost-shadow-virtqueue.c | 1 +
>>   1 file changed, 1 insertion(+)
>>
>> diff --git a/hw/virtio/vhost-shadow-virtqueue.c b/hw/virtio/vhost-shadow-virtqueue.c
>> index 430729635815..31cf642db267 100644
>> --- a/hw/virtio/vhost-shadow-virtqueue.c
>> +++ b/hw/virtio/vhost-shadow-virtqueue.c
>> @@ -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
>>
> 



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-02-17 11:44 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-02-17 10:42 [PATCH] vhost: svq: fix uninitialized variable Laurent Vivier
2023-02-17 11:24 ` Eugenio Perez Martin
2023-02-17 11:44   ` Laurent Vivier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).