vhost: use struct_size and size_add to compute flex array sizes
Subsystems (3)
PATCH 1/1 vhost: use struct_size and size_add to compute flex array sizes
The vhost_get_avail_size and vhost_get_used_size functions compute the size of structures with flexible array members with an additional 2 bytes if the VIRTIO_RING_F_EVENT_IDX feature flag is set. Convert these functions to use struct_size() and size_add() instead of coding the calculation by hand.
This ensures that the calculations will saturate at SIZE_MAX rather than overflowing.
---
I found this using a coccinelle patch I developed and submitted at [1].
[1]: https://lore.kernel.org/all/[email protected]/
drivers/vhost/vhost.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/vhost/vhost.c +2 -4
@@ -436,8 +436,7 @@ static size_t vhost_get_avail_size(struct vhost_virtqueue *vq,size_t event__maybe_unused =vhost_has_feature (vq,VIRTIO_RING_F_EVENT_IDX ) ?2 :0 ; -return sizeof (*vq->avail) + -sizeof (*vq->avail->ring) * num + event; +return size_add (struct_size (vq->avail, ring, num), event); } static size_t vhost_get_used_size(struct vhost_virtqueue *vq,
@@ -446,8 +445,7 @@ static size_t vhost_get_used_size(struct vhost_virtqueue *vq,size_t event__maybe_unused =vhost_has_feature (vq,VIRTIO_RING_F_EVENT_IDX ) ?2 :0 ; -return sizeof (*vq->used) + -sizeof (*vq->used->ring) * num + event; +return size_add (struct_size (vq->used, ring, num), event); } static size_t vhost_get_desc_size(struct vhost_virtqueue *vq,
Signature · 1 line
2.39.1.405.gd4c25cc71f83
Quoted 5 lines · Jacob Keller
The
vhost_get_avail_sizeandvhost_get_used_sizefunctions compute the size of structures with flexible array members with an additional 2 bytes if theVIRTIO_RING_F_EVENT_IDXfeature flag is set. Convert these functions to usestruct_size()andsize_add()instead of coding the calculation by hand.
Quoted 8 lines
This ensures that the calculations will saturate at
SIZE_MAXrather than overflowing.Signed-off-by Jacob Keller <[email protected]>Cc "Michael S. Tsirkin" <[email protected]>Cc Jason Wang <[email protected]>
Will merge, thanks!
Quoted 35 lines
---
I found this using a coccinelle patch I developed and submitted at [1].
[1]: https://lore.kernel.org/all/[email protected]/
drivers/vhost/vhost.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)drivers/vhost/vhost.c +2
-4@@ -436,8 +436,7 @@ static size_t vhost_get_avail_size(struct vhost_virtqueue *vq,size_t event__maybe_unused =vhost_has_feature (vq,VIRTIO_RING_F_EVENT_IDX ) ?2 :0 ; -return sizeof (*vq->avail) + -sizeof (*vq->avail->ring) * num + event; +return size_add (struct_size (vq->avail, ring, num), event); } static size_t vhost_get_used_size(struct vhost_virtqueue *vq,@@ -446,8 +445,7 @@ static size_t vhost_get_used_size(struct vhost_virtqueue *vq,size_t event__maybe_unused =vhost_has_feature (vq,VIRTIO_RING_F_EVENT_IDX ) ?2 :0 ; -return sizeof (*vq->used) + -sizeof (*vq->used->ring) * num + event; +return size_add (struct_size (vq->used, ring, num), event); } static size_t vhost_get_desc_size(struct vhost_virtqueue *vq,Signature · 1 line
2.39.1.405.gd4c25cc71f83
_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization