linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: Robin Murphy <robin.murphy@arm.com>
To: joro@8bytes.org
Cc: iommu@lists.linux-foundation.org,
	David Laight <David.Laight@ACULAB.COM>,
	linux-arm-kernel@lists.infradead.org
Subject: [PATCH] iommu: Fix offsetof() usage
Date: Mon,  8 Apr 2019 13:41:47 +0100	[thread overview]
Message-ID: <85778a4619eab832eb0b003a68f377aea9d97b22.1554727307.git.robin.murphy@arm.com> (raw)

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(-)

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)
-- 
2.21.0.dirty


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

             reply	other threads:[~2019-04-08 12:42 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-08 12:41 Robin Murphy [this message]
2019-04-09 10:00 ` [PATCH] iommu: Fix offsetof() usage Christoph Hellwig

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=85778a4619eab832eb0b003a68f377aea9d97b22.1554727307.git.robin.murphy@arm.com \
    --to=robin.murphy@arm.com \
    --cc=David.Laight@ACULAB.COM \
    --cc=iommu@lists.linux-foundation.org \
    --cc=joro@8bytes.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).