diff mbox series

iommu: Fix offsetof() usage

Message ID 85778a4619eab832eb0b003a68f377aea9d97b22.1554727307.git.robin.murphy@arm.com (mailing list archive)
State New, archived
Headers show
Series iommu: Fix offsetof() usage | expand

Commit Message

Robin Murphy April 8, 2019, 12:41 p.m. UTC
David rightly points out that, although GCC lets us get away with it,
using offsetof() with a non-constant member designator is not compliant
with the C standard. I'm responsible for a couple of those misuses, but
both in circumstances for which we now have a specific helper, so we can
use that to put things straight.

Suggested-by: David Laight <David.Laight@ACULAB.COM>
Signed-off-by: Robin Murphy <robin.murphy@arm.com>
---
 drivers/iommu/arm-smmu.c | 3 +--
 drivers/iommu/iommu.c    | 2 +-
 2 files changed, 2 insertions(+), 3 deletions(-)

Comments

Christoph Hellwig April 9, 2019, 10 a.m. UTC | #1
> index 74e944bd4a8d..81d449451494 100644
> --- a/drivers/iommu/arm-smmu.c
> +++ b/drivers/iommu/arm-smmu.c
> @@ -1484,8 +1484,7 @@ static int arm_smmu_add_device(struct device *dev)
>  	}
>  
>  	ret = -ENOMEM;
> -	cfg = kzalloc(offsetof(struct arm_smmu_master_cfg, smendx[i]),
> -		      GFP_KERNEL);
> +	cfg = kzalloc(struct_size(cfg, smendx, i), GFP_KERNEL);

This looks like a huge improvement, but I find the usage of i
here still very obsfucating.  Can we please use fwspec->num_ids
directly instead of make it much more obvious?
diff mbox series

Patch

diff --git a/drivers/iommu/arm-smmu.c b/drivers/iommu/arm-smmu.c
index 74e944bd4a8d..81d449451494 100644
--- a/drivers/iommu/arm-smmu.c
+++ b/drivers/iommu/arm-smmu.c
@@ -1484,8 +1484,7 @@  static int arm_smmu_add_device(struct device *dev)
 	}
 
 	ret = -ENOMEM;
-	cfg = kzalloc(offsetof(struct arm_smmu_master_cfg, smendx[i]),
-		      GFP_KERNEL);
+	cfg = kzalloc(struct_size(cfg, smendx, i), GFP_KERNEL);
 	if (!cfg)
 		goto out_free;
 
diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c
index 33a982e33716..ad28919e1452 100644
--- a/drivers/iommu/iommu.c
+++ b/drivers/iommu/iommu.c
@@ -2021,7 +2021,7 @@  int iommu_fwspec_add_ids(struct device *dev, u32 *ids, int num_ids)
 	if (!fwspec)
 		return -EINVAL;
 
-	size = offsetof(struct iommu_fwspec, ids[fwspec->num_ids + num_ids]);
+	size = struct_size(fwspec, ids, fwspec->num_ids + num_ids);
 	if (size > sizeof(*fwspec)) {
 		fwspec = krealloc(fwspec, size, GFP_KERNEL);
 		if (!fwspec)