iio: Use kmalloc_array() in iio_scan_mask_set() [Linux 3.16.72]

This Linux kernel change "iio: Use kmalloc_array() in iio_scan_mask_set()" is included in the Linux 3.16.72 release. This change is authored by Markus Elfring <elfring [at] users.sourceforge.net> on Fri Sep 23 22:30:32 2016 +0200. The commit for this change in Linux stable tree is 326b6ad (patch) which is from upstream commit 057ac1a. The same Linux upstream change may have been applied to various maintained Linux releases and you can find all Linux releases containing changes from upstream 057ac1a.

iio: Use kmalloc_array() in iio_scan_mask_set()

commit 057ac1acdfc4743f066fcefe359385cad00549eb upstream.

A multiplication for the size determination of a memory allocation
indicated that an array data structure should be processed.
Thus use the corresponding function "kmalloc_array".

This issue was detected by using the Coccinelle software.

Signed-off-by: Markus Elfring <[email protected]>
Signed-off-by: Jonathan Cameron <[email protected]>
Signed-off-by: Ben Hutchings <[email protected]>

There are 7 lines of Linux source code added/deleted in this change. Code changes to Linux kernel are as follows.

 drivers/iio/industrialio-buffer.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/drivers/iio/industrialio-buffer.c b/drivers/iio/industrialio-buffer.c
index 7562531..7613668 100644
--- a/drivers/iio/industrialio-buffer.c
+++ b/drivers/iio/industrialio-buffer.c
@@ -836,10 +836,9 @@ int iio_scan_mask_set(struct iio_dev *indio_dev,
    const unsigned long *mask;
    unsigned long *trialmask;

-   trialmask = kmalloc(sizeof(*trialmask)*
-               BITS_TO_LONGS(indio_dev->masklength),
-               GFP_KERNEL);
+   trialmask = kmalloc_array(BITS_TO_LONGS(indio_dev->masklength),
+                 sizeof(*trialmask),
+                 GFP_KERNEL);
    if (trialmask == NULL)
        return -ENOMEM;
    if (!indio_dev->masklength) {

Leave a Reply

Your email address will not be published. Required fields are marked *