Skip to contentSkip to conversation

drivers/iommu: use struct_size over open coded arithmetic

PATCH New [email protected] 1 participant 1 message Feb 16, 2022

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

PATCH 1/1 drivers/iommu: use struct_size over open coded arithmetic

From: Minghao Chi (CGEL ZTE) <[email protected]>

Replace zero-length array with flexible-array member and make use of the struct_size() helper in kzalloc(). For example:

struct viommu_request {
	...
	unsigned int			len;
	char				buf[];
};

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

---
 drivers/iommu/virtio-iommu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iommu/virtio-iommu.c +1 -1
index f2aa34f57454..0996d9c7c358 100644
@@ -231,7 +231,7 @@ static int __viommu_add_req(struct viommu_dev *viommu, void *buf, size_t len,
 	if (write_offset <= 0)
 		return -EINVAL;
 
-	req = kzalloc(sizeof(*req) + len, GFP_ATOMIC);
+	req = kzalloc(struct_size(req, buf, len), GFP_ATOMIC);
 	if (!req)
 		return -ENOMEM;
 
Signature · 1 line
2.25.1