Skip to contentSkip to conversation

drm/nouveau/mmu: use struct_size() helper

PATCH In review Gustavo A. R. Silva 2 participants 2 messages May 24, 2019 – May 27, 2019

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

PATCH 1/1 drm/nouveau/mmu: use struct_size() helper

Make use of the struct_size() helper instead of an open-coded version in order to avoid any potential type mistakes, in particular in the context in which this code is being used.

So, replace the following form:

sizeof(*kind) + sizeof(*kind->data) * mmu->kind_nr;

with:

struct_size(kind, data, mmu->kind_nr)

This code was detected with the help of Coccinelle.

---
 drivers/gpu/drm/nouveau/nvif/mmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/nouveau/nvif/mmu.c +1 -1
index ae08a1ca8044..5641bda2046d 100644
@@ -110,7 +110,7 @@ nvif_mmu_init(struct nvif_object *parent, s32 oclass, struct nvif_mmu *mmu)
 
 	if (mmu->kind_nr) {
 		struct nvif_mmu_kind_v0 *kind;
-		u32 argc = sizeof(*kind) + sizeof(*kind->data) * mmu->kind_nr;
+		size_t argc = struct_size(kind, data, mmu->kind_nr);
 
 		if (ret = -ENOMEM, !(kind = kmalloc(argc, GFP_KERNEL)))
 			goto done;
Signature · 1 line
2.21.0
#1 lore raw
Quoted 9 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, in particular in the context in which this code is being used.

So, replace the following form:

sizeof(*kind) + sizeof(*kind->data) * mmu->kind_nr;

Quoted 7 lines

with:

struct_size(kind, data, mmu->kind_nr)

This code was detected with the help of Coccinelle.

Thanks!

Quoted 20 lines
---
 drivers/gpu/drm/nouveau/nvif/mmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/nouveau/nvif/mmu.c +1 -2
index ae08a1ca8044..5641bda2046d 100644
@@ -110,7 +110,7 @@ nvif_mmu_init(struct nvif_object *parent, s32 oclass, struct nvif_mmu *mmu)
 
        if (mmu->kind_nr) {
                struct nvif_mmu_kind_v0 *kind;
-               u32 argc = sizeof(*kind) + sizeof(*kind->data) * mmu->kind_nr;
+               size_t argc = struct_size(kind, data, mmu->kind_nr);
 
                if (ret = -ENOMEM, !(kind = kmalloc(argc, GFP_KERNEL)))
                        goto done;
--

2.21.0

Quoted 4 lines

_______________________________________________
Nouveau mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/nouveau

_______________________________________________
dri-devel mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/dri-devel