Re: [PATCH v2] iio: hmc5843: fix potential NULL pointer dereferences

2019-03-24 Thread Jonathan Cameron
On Sun, 24 Mar 2019 10:48:56 +
Jonathan Cameron  wrote:

> On Sat, 16 Mar 2019 17:08:33 -0500
> Kangjie Lu  wrote:
> 
> > devm_regmap_init_i2c may fail and return NULL. The fix returns
> > the error when it fails.
> > 
> > Signed-off-by: Kangjie Lu   
> Applied to the togreg branch of iio.git and pushed out as testing
> for the autobuilders to play with it.
> Thanks.

Ah, how the eye jumps over the obvious...

Good thing this one was caught by build tests.

See below.

I've fixed up in the tree.

Jonathan

> 
> Jonathan
> 
> > ---
> > V2: fix the two together
> > ---
> >  drivers/iio/magnetometer/hmc5843_i2c.c | 7 ++-
> >  drivers/iio/magnetometer/hmc5843_spi.c | 7 ++-
> >  2 files changed, 12 insertions(+), 2 deletions(-)
> > 
> > diff --git a/drivers/iio/magnetometer/hmc5843_i2c.c 
> > b/drivers/iio/magnetometer/hmc5843_i2c.c
> > index 3de7f4426ac4..86abba5827a2 100644
> > --- a/drivers/iio/magnetometer/hmc5843_i2c.c
> > +++ b/drivers/iio/magnetometer/hmc5843_i2c.c
> > @@ -58,8 +58,13 @@ static const struct regmap_config 
> > hmc5843_i2c_regmap_config = {
> >  static int hmc5843_i2c_probe(struct i2c_client *cli,
> >  const struct i2c_device_id *id)
> >  {
> > +   struct regmap *regmap = devm_regmap_init_i2c(cli,
> > +   _i2c_regmap_config);
> > +   if (IS_ERR(regmap))
> > +   return PTR_ERR(regmap);
> > +
> > return hmc5843_common_probe(>dev,
> > -   devm_regmap_init_i2c(cli, _i2c_regmap_config),
> > +   regmap,
> > id->driver_data, id->name);
> >  }
> >  
> > diff --git a/drivers/iio/magnetometer/hmc5843_spi.c 
> > b/drivers/iio/magnetometer/hmc5843_spi.c
> > index 535f03a70d63..8355713651d4 100644
> > --- a/drivers/iio/magnetometer/hmc5843_spi.c
> > +++ b/drivers/iio/magnetometer/hmc5843_spi.c
> > @@ -58,6 +58,7 @@ static const struct regmap_config 
> > hmc5843_spi_regmap_config = {
> >  static int hmc5843_spi_probe(struct spi_device *spi)
> >  {
> > int ret;
> > +   struct regmap *regmap;
> > const struct spi_device_id *id = spi_get_device_id(spi);
> >  
> > spi->mode = SPI_MODE_3;
> > @@ -67,8 +68,12 @@ static int hmc5843_spi_probe(struct spi_device *spi)
> > if (ret)
> > return ret;
> >  
> > +   regmap = devm_regmap_init(spi, _spi_regmap_config);

devm_regmap_init_spi


> > +   if (IS_ERR(regmap))
> > +   return PTR_ERR(devm_regmap);

It's called regmap.

> > +
> > return hmc5843_common_probe(>dev,
> > -   devm_regmap_init_spi(spi, _spi_regmap_config),
> > +   regmap,
> > id->driver_data, id->name);
> >  }
> >
> 



Re: [PATCH v2] iio: hmc5843: fix potential NULL pointer dereferences

2019-03-24 Thread Jonathan Cameron
On Sat, 16 Mar 2019 17:08:33 -0500
Kangjie Lu  wrote:

> devm_regmap_init_i2c may fail and return NULL. The fix returns
> the error when it fails.
> 
> Signed-off-by: Kangjie Lu 
Applied to the togreg branch of iio.git and pushed out as testing
for the autobuilders to play with it.
Thanks.

Jonathan

> ---
> V2: fix the two together
> ---
>  drivers/iio/magnetometer/hmc5843_i2c.c | 7 ++-
>  drivers/iio/magnetometer/hmc5843_spi.c | 7 ++-
>  2 files changed, 12 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/iio/magnetometer/hmc5843_i2c.c 
> b/drivers/iio/magnetometer/hmc5843_i2c.c
> index 3de7f4426ac4..86abba5827a2 100644
> --- a/drivers/iio/magnetometer/hmc5843_i2c.c
> +++ b/drivers/iio/magnetometer/hmc5843_i2c.c
> @@ -58,8 +58,13 @@ static const struct regmap_config 
> hmc5843_i2c_regmap_config = {
>  static int hmc5843_i2c_probe(struct i2c_client *cli,
>const struct i2c_device_id *id)
>  {
> + struct regmap *regmap = devm_regmap_init_i2c(cli,
> + _i2c_regmap_config);
> + if (IS_ERR(regmap))
> + return PTR_ERR(regmap);
> +
>   return hmc5843_common_probe(>dev,
> - devm_regmap_init_i2c(cli, _i2c_regmap_config),
> + regmap,
>   id->driver_data, id->name);
>  }
>  
> diff --git a/drivers/iio/magnetometer/hmc5843_spi.c 
> b/drivers/iio/magnetometer/hmc5843_spi.c
> index 535f03a70d63..8355713651d4 100644
> --- a/drivers/iio/magnetometer/hmc5843_spi.c
> +++ b/drivers/iio/magnetometer/hmc5843_spi.c
> @@ -58,6 +58,7 @@ static const struct regmap_config hmc5843_spi_regmap_config 
> = {
>  static int hmc5843_spi_probe(struct spi_device *spi)
>  {
>   int ret;
> + struct regmap *regmap;
>   const struct spi_device_id *id = spi_get_device_id(spi);
>  
>   spi->mode = SPI_MODE_3;
> @@ -67,8 +68,12 @@ static int hmc5843_spi_probe(struct spi_device *spi)
>   if (ret)
>   return ret;
>  
> + regmap = devm_regmap_init(spi, _spi_regmap_config);
> + if (IS_ERR(regmap))
> + return PTR_ERR(devm_regmap);
> +
>   return hmc5843_common_probe(>dev,
> - devm_regmap_init_spi(spi, _spi_regmap_config),
> + regmap,
>   id->driver_data, id->name);
>  }
>  



[PATCH v2] iio: hmc5843: fix potential NULL pointer dereferences

2019-03-16 Thread Kangjie Lu
devm_regmap_init_i2c may fail and return NULL. The fix returns
the error when it fails.

Signed-off-by: Kangjie Lu 
---
V2: fix the two together
---
 drivers/iio/magnetometer/hmc5843_i2c.c | 7 ++-
 drivers/iio/magnetometer/hmc5843_spi.c | 7 ++-
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/drivers/iio/magnetometer/hmc5843_i2c.c 
b/drivers/iio/magnetometer/hmc5843_i2c.c
index 3de7f4426ac4..86abba5827a2 100644
--- a/drivers/iio/magnetometer/hmc5843_i2c.c
+++ b/drivers/iio/magnetometer/hmc5843_i2c.c
@@ -58,8 +58,13 @@ static const struct regmap_config hmc5843_i2c_regmap_config 
= {
 static int hmc5843_i2c_probe(struct i2c_client *cli,
 const struct i2c_device_id *id)
 {
+   struct regmap *regmap = devm_regmap_init_i2c(cli,
+   _i2c_regmap_config);
+   if (IS_ERR(regmap))
+   return PTR_ERR(regmap);
+
return hmc5843_common_probe(>dev,
-   devm_regmap_init_i2c(cli, _i2c_regmap_config),
+   regmap,
id->driver_data, id->name);
 }
 
diff --git a/drivers/iio/magnetometer/hmc5843_spi.c 
b/drivers/iio/magnetometer/hmc5843_spi.c
index 535f03a70d63..8355713651d4 100644
--- a/drivers/iio/magnetometer/hmc5843_spi.c
+++ b/drivers/iio/magnetometer/hmc5843_spi.c
@@ -58,6 +58,7 @@ static const struct regmap_config hmc5843_spi_regmap_config = 
{
 static int hmc5843_spi_probe(struct spi_device *spi)
 {
int ret;
+   struct regmap *regmap;
const struct spi_device_id *id = spi_get_device_id(spi);
 
spi->mode = SPI_MODE_3;
@@ -67,8 +68,12 @@ static int hmc5843_spi_probe(struct spi_device *spi)
if (ret)
return ret;
 
+   regmap = devm_regmap_init(spi, _spi_regmap_config);
+   if (IS_ERR(regmap))
+   return PTR_ERR(devm_regmap);
+
return hmc5843_common_probe(>dev,
-   devm_regmap_init_spi(spi, _spi_regmap_config),
+   regmap,
id->driver_data, id->name);
 }
 
-- 
2.17.1