All of lore.kernel.org
 help / color / mirror / Atom feed
From: Dmitry Torokhov <dmitry.torokhov@gmail.com>
To: Richard Cochran <richardcochran@gmail.com>
Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH 2/4] ptp: use kcalloc/kmallco_array when allocating arrays
Date: Mon, 13 Feb 2017 19:51:06 -0800	[thread overview]
Message-ID: <20170214035108.19622-2-dmitry.torokhov@gmail.com> (raw)
In-Reply-To: <20170214035108.19622-1-dmitry.torokhov@gmail.com>

kcalloc/kmalloc_array are more semantically correct when allocating arrays
of objects, and overflow-safe.

Signed-off-by: Dmitry Torokhov <dmitry.torokhov@gmail.com>
---
 drivers/ptp/ptp_sysfs.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/drivers/ptp/ptp_sysfs.c b/drivers/ptp/ptp_sysfs.c
index 53d43954a974..77843790c381 100644
--- a/drivers/ptp/ptp_sysfs.c
+++ b/drivers/ptp/ptp_sysfs.c
@@ -269,13 +269,13 @@ static int ptp_populate_pins(struct ptp_clock *ptp)
 	struct ptp_clock_info *info = ptp->info;
 	int err = -ENOMEM, i, n_pins = info->n_pins;
 
-	ptp->pin_dev_attr = kzalloc(n_pins * sizeof(*ptp->pin_dev_attr),
+	ptp->pin_dev_attr = kcalloc(n_pins, sizeof(*ptp->pin_dev_attr),
 				    GFP_KERNEL);
 	if (!ptp->pin_dev_attr)
 		goto no_dev_attr;
 
-	ptp->pin_attr = kzalloc((1 + n_pins) * sizeof(struct attribute *),
-				GFP_KERNEL);
+	ptp->pin_attr = kmalloc_array(1 + n_pins, sizeof(*ptp->pin_attr),
+				      GFP_KERNEL);
 	if (!ptp->pin_attr)
 		goto no_pin_attr;
 
@@ -289,6 +289,9 @@ static int ptp_populate_pins(struct ptp_clock *ptp)
 		ptp->pin_attr[i] = &da->attr;
 	}
 
+	/* NULL terminator */
+	ptp->pin_attr[n_pins] = NULL;
+
 	ptp->pin_attr_group.name = "pins";
 	ptp->pin_attr_group.attrs = ptp->pin_attr;
 
-- 
2.11.0.483.g087da7b7c-goog

  reply	other threads:[~2017-02-14  3:52 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-14  3:51 [PATCH 1/4] ptp: do not explicitly set drvdata in ptp_clock_register() Dmitry Torokhov
2017-02-14  3:51 ` Dmitry Torokhov [this message]
2017-02-14  8:38   ` [PATCH 2/4] ptp: use kcalloc/kmallco_array when allocating arrays Richard Cochran
2017-02-14  3:51 ` [PATCH 3/4] ptp: use is_visible method to hide unused attributes Dmitry Torokhov
2017-02-14  8:42   ` Richard Cochran
2017-02-14 18:07     ` Dmitry Torokhov
2017-02-14 18:13       ` Richard Cochran
2017-02-14  3:51 ` [PATCH 4/4] ptp: create "pins" together with the rest of attributes Dmitry Torokhov
2017-02-14  8:30 ` [PATCH 1/4] ptp: do not explicitly set drvdata in ptp_clock_register() Richard Cochran

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=20170214035108.19622-2-dmitry.torokhov@gmail.com \
    --to=dmitry.torokhov@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=richardcochran@gmail.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.