public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
To: Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org
Cc: Jonathan Cameron <jic23@kernel.org>,
	Lars-Peter Clausen <lars@metafoo.de>,
	Nuno Sa <nuno.sa@analog.com>
Subject: [PATCH v2 3/8] iio: core: Switch to krealloc_array()
Date: Fri, 21 Jul 2023 20:00:17 +0300	[thread overview]
Message-ID: <20230721170022.3461-4-andriy.shevchenko@linux.intel.com> (raw)
In-Reply-To: <20230721170022.3461-1-andriy.shevchenko@linux.intel.com>

Let the krealloc_array() copy the original data and
check for a multiplication overflow.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Reviewed-by: Nuno Sa <nuno.sa@analog.com>
---
 drivers/iio/industrialio-core.c | 20 +++++++-------------
 1 file changed, 7 insertions(+), 13 deletions(-)

diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c
index 4e45740331ee..6e28c2a3d223 100644
--- a/drivers/iio/industrialio-core.c
+++ b/drivers/iio/industrialio-core.c
@@ -1465,7 +1465,7 @@ int iio_device_register_sysfs_group(struct iio_dev *indio_dev,
 	const struct attribute_group **new, **old = iio_dev_opaque->groups;
 	unsigned int cnt = iio_dev_opaque->groupcounter;
 
-	new = krealloc(old, sizeof(*new) * (cnt + 2), GFP_KERNEL);
+	new = krealloc_array(old, cnt + 2, sizeof(*new), GFP_KERNEL);
 	if (!new)
 		return -ENOMEM;
 
@@ -1483,13 +1483,13 @@ static int iio_device_register_sysfs(struct iio_dev *indio_dev)
 {
 	struct iio_dev_opaque *iio_dev_opaque = to_iio_dev_opaque(indio_dev);
 	int i, ret = 0, attrcount, attrn, attrcount_orig = 0;
+	struct attribute **attrs, **attr, *clk = NULL;
 	struct iio_dev_attr *p;
-	struct attribute **attr, *clk = NULL;
 
 	/* First count elements in any existing group */
-	if (indio_dev->info->attrs) {
-		attr = indio_dev->info->attrs->attrs;
-		while (*attr++ != NULL)
+	attrs = indio_dev->info->attrs ? indio_dev->info->attrs->attrs : NULL;
+	if (attrs) {
+		for (attr = attrs; *attr; attr++)
 			attrcount_orig++;
 	}
 	attrcount = attrcount_orig;
@@ -1521,20 +1521,14 @@ static int iio_device_register_sysfs(struct iio_dev *indio_dev)
 	if (clk)
 		attrcount++;
 
+	/* Copy across original attributes, and point to original binary attributes */
 	iio_dev_opaque->chan_attr_group.attrs =
-		kcalloc(attrcount + 1,
-			sizeof(iio_dev_opaque->chan_attr_group.attrs[0]),
-			GFP_KERNEL);
+		krealloc_array(attrs, attrcount + 1, sizeof(*attrs), GFP_KERNEL);
 	if (iio_dev_opaque->chan_attr_group.attrs == NULL) {
 		ret = -ENOMEM;
 		goto error_clear_attrs;
 	}
-	/* Copy across original attributes, and point to original binary attributes */
 	if (indio_dev->info->attrs) {
-		memcpy(iio_dev_opaque->chan_attr_group.attrs,
-		       indio_dev->info->attrs->attrs,
-		       sizeof(iio_dev_opaque->chan_attr_group.attrs[0])
-		       *attrcount_orig);
 		iio_dev_opaque->chan_attr_group.is_visible =
 			indio_dev->info->attrs->is_visible;
 		iio_dev_opaque->chan_attr_group.bin_attrs =
-- 
2.40.0.1.gaa8946217a0b


  parent reply	other threads:[~2023-07-21 17:00 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-21 17:00 [PATCH v2 0/8] iio: core: A few code cleanups and documentation fixes Andy Shevchenko
2023-07-21 17:00 ` [PATCH v2 1/8] iio: core: Add opaque_struct_size() helper and use it Andy Shevchenko
2023-07-21 17:22   ` Andy Shevchenko
2023-07-21 17:25     ` Andy Shevchenko
2023-07-21 17:27       ` Andy Shevchenko
2023-07-21 18:12       ` Uwe Kleine-König
2023-07-21 17:00 ` [PATCH v2 2/8] iio: core: Use sysfs_match_string() helper Andy Shevchenko
2023-07-22 17:11   ` Jonathan Cameron
2023-07-21 17:00 ` Andy Shevchenko [this message]
2023-07-22 17:28   ` [PATCH v2 3/8] iio: core: Switch to krealloc_array() Jonathan Cameron
2023-07-24 10:37     ` Andy Shevchenko
2023-07-21 17:00 ` [PATCH v2 4/8] iio: core: Use min() instead of min_t() to make code more robust Andy Shevchenko
2023-07-23  9:14   ` Jonathan Cameron
2023-07-21 17:00 ` [PATCH v2 5/8] iio: core: Get rid of redundant 'else' Andy Shevchenko
2023-07-23  9:16   ` Jonathan Cameron
2023-07-21 17:00 ` [PATCH v2 6/8] iio: core: Fix issues and style of the comments Andy Shevchenko
2023-07-21 20:48   ` Randy Dunlap
2023-07-21 21:37     ` andy.shevchenko
2023-07-23  9:21   ` Jonathan Cameron
2023-07-21 17:00 ` [PATCH v2 7/8] iio: core: Move initcalls closer to the respective calls Andy Shevchenko
2023-07-23  9:23   ` Jonathan Cameron
2023-07-24 10:53     ` Andy Shevchenko
2023-07-21 17:00 ` [PATCH v2 8/8] iio: core: Improve indentation in a few places Andy Shevchenko
2023-07-23  9:24   ` Jonathan Cameron

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=20230721170022.3461-4-andriy.shevchenko@linux.intel.com \
    --to=andriy.shevchenko@linux.intel.com \
    --cc=jic23@kernel.org \
    --cc=lars@metafoo.de \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nuno.sa@analog.com \
    /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