Skip to contentSkip to conversation

drm/virtio: Use struct_size() helper in kmalloc()

PATCH Applied Gustavo A. R. Silva 2 participants 2 messages Jun 17, 2020 – Aug 17, 2020

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/virtio: Use struct_size() helper in kmalloc()

Make use of the struct_size() helper instead of an open-coded version in order to avoid any potential type mistakes.

This code was detected with the help of Coccinelle and, audited and fixed manually.

---
 drivers/gpu/drm/virtio/virtgpu_gem.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/gpu/drm/virtio/virtgpu_gem.c +1 -2
index 24ffacac99e4..c30c75ee83fc 100644
@@ -154,9 +154,8 @@ void virtio_gpu_gem_object_close(struct drm_gem_object *obj,
 struct virtio_gpu_object_array *virtio_gpu_array_alloc(u32 nents)
 {
 	struct virtio_gpu_object_array *objs;
-	size_t size = sizeof(*objs) + sizeof(objs->objs[0]) * nents;
 
-	objs = kmalloc(size, GFP_KERNEL);
+	objs = kmalloc(struct_size(objs, objs, nents), GFP_KERNEL);
 	if (!objs)
 		return NULL;
 
Signature · 6 lines
2.27.0

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel
maintainer Applied
#1 lore raw
Quoted 7 lines · Gustavo A. R. Silva

Make use of the struct_size() helper instead of an open-coded version in order to avoid any potential type mistakes.

This code was detected with the help of Coccinelle and, audited and fixed manually.

Queued up for drm-misc-next.

thanks,
Gerd