diff mbox series

[next] iio: st_sensors: handle memory allocation failure to fix null pointer dereference

Message ID 20200121161757.1498082-1-colin.king@canonical.com (mailing list archive)
State New, archived
Headers show
Series [next] iio: st_sensors: handle memory allocation failure to fix null pointer dereference | expand

Commit Message

Colin King Jan. 21, 2020, 4:17 p.m. UTC
From: Colin Ian King <colin.king@canonical.com>

A null pointer deference on pdata can occur if the allocation of
pdata fails.  Fix this by adding a null pointer check and handle
the -ENOMEM failure in the caller.

Addresses-Coverity: ("Dereference null return value")
Fixes: 3ce85cc4fbb7 ("iio: st_sensors: get platform data from device tree")
Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 drivers/iio/common/st_sensors/st_sensors_core.c | 4 ++++
 1 file changed, 4 insertions(+)

Comments

Linus Walleij Jan. 23, 2020, 3:28 p.m. UTC | #1
On Tue, Jan 21, 2020 at 5:18 PM Colin King <colin.king@canonical.com> wrote:

> From: Colin Ian King <colin.king@canonical.com>
>
> A null pointer deference on pdata can occur if the allocation of
> pdata fails.  Fix this by adding a null pointer check and handle
> the -ENOMEM failure in the caller.
>
> Addresses-Coverity: ("Dereference null return value")

That's a weirdo tag, but I suppose you have aligned with the maintainers
about this.

> Fixes: 3ce85cc4fbb7 ("iio: st_sensors: get platform data from device tree")
> Signed-off-by: Colin Ian King <colin.king@canonical.com>

Reviewed-by: Linus Walleij <linus.walleij@linaro.org>

Yours,
Linus Walleij
Jonathan Cameron Feb. 2, 2020, 4:25 p.m. UTC | #2
On Thu, 23 Jan 2020 16:28:28 +0100
Linus Walleij <linus.walleij@linaro.org> wrote:

> On Tue, Jan 21, 2020 at 5:18 PM Colin King <colin.king@canonical.com> wrote:
> 
> > From: Colin Ian King <colin.king@canonical.com>
> >
> > A null pointer deference on pdata can occur if the allocation of
> > pdata fails.  Fix this by adding a null pointer check and handle
> > the -ENOMEM failure in the caller.
> >
> > Addresses-Coverity: ("Dereference null return value")  
> 
> That's a weirdo tag, but I suppose you have aligned with the maintainers
> about this.
> 
> > Fixes: 3ce85cc4fbb7 ("iio: st_sensors: get platform data from device tree")
> > Signed-off-by: Colin Ian King <colin.king@canonical.com>  
> 
> Reviewed-by: Linus Walleij <linus.walleij@linaro.org>

Applied to the togreg branch of iio.git and pushed out as testing for
the autobuilders to play with it.

Thanks,

Jonathan

> 
> Yours,
> Linus Walleij
diff mbox series

Patch

diff --git a/drivers/iio/common/st_sensors/st_sensors_core.c b/drivers/iio/common/st_sensors/st_sensors_core.c
index e051edbc43c1..0e35ff06f9af 100644
--- a/drivers/iio/common/st_sensors/st_sensors_core.c
+++ b/drivers/iio/common/st_sensors/st_sensors_core.c
@@ -328,6 +328,8 @@  static struct st_sensors_platform_data *st_sensors_dev_probe(struct device *dev,
 		return NULL;
 
 	pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
+	if (!pdata)
+		return ERR_PTR(-ENOMEM);
 	if (!device_property_read_u32(dev, "st,drdy-int-pin", &val) && (val <= 2))
 		pdata->drdy_int_pin = (u8) val;
 	else
@@ -371,6 +373,8 @@  int st_sensors_init_sensor(struct iio_dev *indio_dev,
 
 	/* If OF/DT pdata exists, it will take precedence of anything else */
 	of_pdata = st_sensors_dev_probe(indio_dev->dev.parent, pdata);
+	if (IS_ERR(of_pdata))
+		return PTR_ERR(of_pdata);
 	if (of_pdata)
 		pdata = of_pdata;