* [PATCH v2] hw/virtio/vdpa-dev: Check returned value instead of dereferencing @errp
@ 2024-07-16 16:26 Zhao Liu
2024-08-20 10:55 ` Michael S. Tsirkin
0 siblings, 1 reply; 3+ messages in thread
From: Zhao Liu @ 2024-07-16 16:26 UTC (permalink / raw)
To: Eugenio Pérez, qemu-devel; +Cc: Zhao Liu, Michael S. Tsirkin, Jason Wang
As the comment in qapi/error, dereferencing @errp requires
ERRP_GUARD():
* = Why, when and how to use ERRP_GUARD() =
*
* Without ERRP_GUARD(), use of the @errp parameter is restricted:
* - It must not be dereferenced, because it may be null.
...
* ERRP_GUARD() lifts these restrictions.
*
* To use ERRP_GUARD(), add it right at the beginning of the function.
* @errp can then be used without worrying about the argument being
* NULL or &error_fatal.
*
* Using it when it's not needed is safe, but please avoid cluttering
* the source with useless code.
Though vhost_vdpa_device_realize() is called at DeviceClass.realize()
context and won't get NULL @errp, it's still better to follow the
requirement to add the ERRP_GUARD().
But qemu_open() and vhost_vdpa_device_get_u32()'s return values can
distinguish between successful and unsuccessful calls, so check the
return values directly without dereferencing @errp, which eliminates
the need of ERRP_GUARD().
Cc: "Michael S. Tsirkin" <mst@redhat.com>
Cc: Jason Wang <jasowang@redhat.com>
Acked-by: Eugenio Pérez <eperezma@redhat.com>
Signed-off-by: Zhao Liu <zhao1.liu@intel.com>
---
v2:
* Added a/b from Eugenio.
* Deleted unnecessary ERRP_GUARD(). (Eugenio)
---
hw/virtio/vdpa-dev.c | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/hw/virtio/vdpa-dev.c b/hw/virtio/vdpa-dev.c
index 64b96b226c39..8a1e16fce3de 100644
--- a/hw/virtio/vdpa-dev.c
+++ b/hw/virtio/vdpa-dev.c
@@ -63,19 +63,19 @@ static void vhost_vdpa_device_realize(DeviceState *dev, Error **errp)
}
v->vhostfd = qemu_open(v->vhostdev, O_RDWR, errp);
- if (*errp) {
+ if (v->vhostfd < 0) {
return;
}
v->vdev_id = vhost_vdpa_device_get_u32(v->vhostfd,
VHOST_VDPA_GET_DEVICE_ID, errp);
- if (*errp) {
+ if (v->vdev_id < 0) {
goto out;
}
max_queue_size = vhost_vdpa_device_get_u32(v->vhostfd,
VHOST_VDPA_GET_VRING_NUM, errp);
- if (*errp) {
+ if (max_queue_size < 0) {
goto out;
}
@@ -89,7 +89,7 @@ static void vhost_vdpa_device_realize(DeviceState *dev, Error **errp)
v->num_queues = vhost_vdpa_device_get_u32(v->vhostfd,
VHOST_VDPA_GET_VQS_COUNT, errp);
- if (*errp) {
+ if (v->num_queues < 0) {
goto out;
}
@@ -127,7 +127,7 @@ static void vhost_vdpa_device_realize(DeviceState *dev, Error **errp)
v->config_size = vhost_vdpa_device_get_u32(v->vhostfd,
VHOST_VDPA_GET_CONFIG_SIZE,
errp);
- if (*errp) {
+ if (v->config_size < 0) {
goto vhost_cleanup;
}
--
2.34.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH v2] hw/virtio/vdpa-dev: Check returned value instead of dereferencing @errp
2024-07-16 16:26 [PATCH v2] hw/virtio/vdpa-dev: Check returned value instead of dereferencing @errp Zhao Liu
@ 2024-08-20 10:55 ` Michael S. Tsirkin
2024-08-31 11:18 ` Zhao Liu
0 siblings, 1 reply; 3+ messages in thread
From: Michael S. Tsirkin @ 2024-08-20 10:55 UTC (permalink / raw)
To: Zhao Liu; +Cc: Eugenio Pérez, qemu-devel, Jason Wang
On Wed, Jul 17, 2024 at 12:26:15AM +0800, Zhao Liu wrote:
> As the comment in qapi/error, dereferencing @errp requires
> ERRP_GUARD():
>
> * = Why, when and how to use ERRP_GUARD() =
> *
> * Without ERRP_GUARD(), use of the @errp parameter is restricted:
> * - It must not be dereferenced, because it may be null.
> ...
> * ERRP_GUARD() lifts these restrictions.
> *
> * To use ERRP_GUARD(), add it right at the beginning of the function.
> * @errp can then be used without worrying about the argument being
> * NULL or &error_fatal.
> *
> * Using it when it's not needed is safe, but please avoid cluttering
> * the source with useless code.
>
> Though vhost_vdpa_device_realize() is called at DeviceClass.realize()
> context and won't get NULL @errp, it's still better to follow the
> requirement to add the ERRP_GUARD().
>
> But qemu_open() and vhost_vdpa_device_get_u32()'s return values can
> distinguish between successful and unsuccessful calls, so check the
> return values directly without dereferencing @errp, which eliminates
> the need of ERRP_GUARD().
>
> Cc: "Michael S. Tsirkin" <mst@redhat.com>
> Cc: Jason Wang <jasowang@redhat.com>
> Acked-by: Eugenio Pérez <eperezma@redhat.com>
> Signed-off-by: Zhao Liu <zhao1.liu@intel.com>
> ---
> v2:
> * Added a/b from Eugenio.
> * Deleted unnecessary ERRP_GUARD(). (Eugenio)
> ---
> hw/virtio/vdpa-dev.c | 10 +++++-----
> 1 file changed, 5 insertions(+), 5 deletions(-)
>
> diff --git a/hw/virtio/vdpa-dev.c b/hw/virtio/vdpa-dev.c
> index 64b96b226c39..8a1e16fce3de 100644
> --- a/hw/virtio/vdpa-dev.c
> +++ b/hw/virtio/vdpa-dev.c
> @@ -63,19 +63,19 @@ static void vhost_vdpa_device_realize(DeviceState *dev, Error **errp)
> }
>
> v->vhostfd = qemu_open(v->vhostdev, O_RDWR, errp);
> - if (*errp) {
> + if (v->vhostfd < 0) {
> return;
> }
>
> v->vdev_id = vhost_vdpa_device_get_u32(v->vhostfd,
> VHOST_VDPA_GET_DEVICE_ID, errp);
> - if (*errp) {
> + if (v->vdev_id < 0) {
> goto out;
> }
vdev_id is unsigned, no idea how is this supposed to work.
>
> max_queue_size = vhost_vdpa_device_get_u32(v->vhostfd,
> VHOST_VDPA_GET_VRING_NUM, errp);
> - if (*errp) {
> + if (max_queue_size < 0) {
> goto out;
> }
>
max_queue_size is unsigned, too.
> @@ -89,7 +89,7 @@ static void vhost_vdpa_device_realize(DeviceState *dev, Error **errp)
>
> v->num_queues = vhost_vdpa_device_get_u32(v->vhostfd,
> VHOST_VDPA_GET_VQS_COUNT, errp);
> - if (*errp) {
> + if (v->num_queues < 0) {
> goto out;
> }
>
num_queues is unsigned, too.
> @@ -127,7 +127,7 @@ static void vhost_vdpa_device_realize(DeviceState *dev, Error **errp)
> v->config_size = vhost_vdpa_device_get_u32(v->vhostfd,
> VHOST_VDPA_GET_CONFIG_SIZE,
> errp);
> - if (*errp) {
> + if (v->config_size < 0) {
> goto vhost_cleanup;
> }
>
> --
> 2.34.1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v2] hw/virtio/vdpa-dev: Check returned value instead of dereferencing @errp
2024-08-20 10:55 ` Michael S. Tsirkin
@ 2024-08-31 11:18 ` Zhao Liu
0 siblings, 0 replies; 3+ messages in thread
From: Zhao Liu @ 2024-08-31 11:18 UTC (permalink / raw)
To: Michael S. Tsirkin; +Cc: Eugenio P�rez, qemu-devel, Jason Wang, Zhao Liu
Hi Michael,
On Tue, Aug 20, 2024 at 06:55:29AM -0400, Michael S. Tsirkin wrote:
[snip]
> > diff --git a/hw/virtio/vdpa-dev.c b/hw/virtio/vdpa-dev.c
> > index 64b96b226c39..8a1e16fce3de 100644
> > --- a/hw/virtio/vdpa-dev.c
> > +++ b/hw/virtio/vdpa-dev.c
> > @@ -63,19 +63,19 @@ static void vhost_vdpa_device_realize(DeviceState *dev, Error **errp)
> > }
> >
> > v->vhostfd = qemu_open(v->vhostdev, O_RDWR, errp);
> > - if (*errp) {
> > + if (v->vhostfd < 0) {
> > return;
> > }
> >
> > v->vdev_id = vhost_vdpa_device_get_u32(v->vhostfd,
> > VHOST_VDPA_GET_DEVICE_ID, errp);
> > - if (*errp) {
> > + if (v->vdev_id < 0) {
> > goto out;
> > }
>
> vdev_id is unsigned, no idea how is this supposed to work.
>
> >
> > max_queue_size = vhost_vdpa_device_get_u32(v->vhostfd,
> > VHOST_VDPA_GET_VRING_NUM, errp);
> > - if (*errp) {
> > + if (max_queue_size < 0) {
> > goto out;
> > }
> >
> max_queue_size is unsigned, too.
>
> > @@ -89,7 +89,7 @@ static void vhost_vdpa_device_realize(DeviceState *dev, Error **errp)
> >
> > v->num_queues = vhost_vdpa_device_get_u32(v->vhostfd,
> > VHOST_VDPA_GET_VQS_COUNT, errp);
> > - if (*errp) {
> > + if (v->num_queues < 0) {
> > goto out;
> > }
> >
>
> num_queues is unsigned, too.
Oops, yes. The correct way is to check whether vhost_vdpa_device_get_u32
returns "(uint32_t)-1".
I can add a new macro like this:
#define VDPA_DEVICE_U32_VALUE_NONE ((uint32_t)-1)
Is this okay with you?
Thanks,
Zhao
> > @@ -127,7 +127,7 @@ static void vhost_vdpa_device_realize(DeviceState *dev, Error **errp)
> > v->config_size = vhost_vdpa_device_get_u32(v->vhostfd,
> > VHOST_VDPA_GET_CONFIG_SIZE,
> > errp);
> > - if (*errp) {
> > + if (v->config_size < 0) {
> > goto vhost_cleanup;
> > }
> >
> > --
> > 2.34.1
>
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-08-31 11:03 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-07-16 16:26 [PATCH v2] hw/virtio/vdpa-dev: Check returned value instead of dereferencing @errp Zhao Liu
2024-08-20 10:55 ` Michael S. Tsirkin
2024-08-31 11:18 ` Zhao Liu
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).