linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] iio: imu: adis16400: release allocated memory on failure
@ 2019-09-18 16:57 Navid Emamdoost
  2019-09-19  6:51 ` Ardelean, Alexandru
  0 siblings, 1 reply; 5+ messages in thread
From: Navid Emamdoost @ 2019-09-18 16:57 UTC (permalink / raw)
  Cc: emamd001, smccaman, kjlu, Navid Emamdoost, Lars-Peter Clausen,
	Michael Hennerich, Stefan Popa, Jonathan Cameron, Hartmut Knaack,
	Peter Meerwald-Stadler, linux-iio, linux-kernel

In adis_update_scan_mode, if allocation for adis->buffer fails,
previously allocated adis->xfer needs to be released.

Signed-off-by: Navid Emamdoost <navid.emamdoost@gmail.com>
---
 drivers/iio/imu/adis_buffer.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/imu/adis_buffer.c b/drivers/iio/imu/adis_buffer.c
index 9ac8356d9a95..c5d7e368a636 100644
--- a/drivers/iio/imu/adis_buffer.c
+++ b/drivers/iio/imu/adis_buffer.c
@@ -78,8 +78,10 @@ int adis_update_scan_mode(struct iio_dev *indio_dev,
 		return -ENOMEM;
 
 	adis->buffer = kcalloc(indio_dev->scan_bytes, 2, GFP_KERNEL);
-	if (!adis->buffer)
+	if (!adis->buffer) {
+		kfree(adis->xfer);
 		return -ENOMEM;
+	}
 
 	rx = adis->buffer;
 	tx = rx + scan_count;
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] iio: imu: adis16400: release allocated memory on failure
  2019-09-18 16:57 [PATCH] iio: imu: adis16400: release allocated memory on failure Navid Emamdoost
@ 2019-09-19  6:51 ` Ardelean, Alexandru
  2019-09-19 15:50   ` [PATCH v2] " Navid Emamdoost
  0 siblings, 1 reply; 5+ messages in thread
From: Ardelean, Alexandru @ 2019-09-19  6:51 UTC (permalink / raw)
  To: navid.emamdoost
  Cc: Popa, Stefan Serban, emamd001, linux-iio, linux-kernel, jic23,
	knaack.h, Hennerich, Michael, lars, smccaman, kjlu, pmeerw

On Wed, 2019-09-18 at 11:57 -0500, Navid Emamdoost wrote:
> [External]
> 

Hey,

Good catch.
One comment inline.

> In adis_update_scan_mode, if allocation for adis->buffer fails,
> previously allocated adis->xfer needs to be released.
> 
> Signed-off-by: Navid Emamdoost <navid.emamdoost@gmail.com>
> ---
>  drivers/iio/imu/adis_buffer.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/iio/imu/adis_buffer.c
> b/drivers/iio/imu/adis_buffer.c
> index 9ac8356d9a95..c5d7e368a636 100644
> --- a/drivers/iio/imu/adis_buffer.c
> +++ b/drivers/iio/imu/adis_buffer.c
> @@ -78,8 +78,10 @@ int adis_update_scan_mode(struct iio_dev *indio_dev,
>  		return -ENOMEM;
>  
>  	adis->buffer = kcalloc(indio_dev->scan_bytes, 2, GFP_KERNEL);
> -	if (!adis->buffer)
> +	if (!adis->buffer) {
> +		kfree(adis->xfer);

Maybe also do  "adis->xfer = NULL" here.
The idea is to make sure that the pointer is not double-free'd by some
other function (i.e. adis_cleanup_buffer_and_trigger() or another
adis_update_scan_mode() call).

>  		return -ENOMEM;
> +	}
>  
>  	rx = adis->buffer;
>  	tx = rx + scan_count;

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2] iio: imu: adis16400: release allocated memory on failure
  2019-09-19  6:51 ` Ardelean, Alexandru
@ 2019-09-19 15:50   ` Navid Emamdoost
  2019-09-20  6:46     ` Ardelean, Alexandru
  0 siblings, 1 reply; 5+ messages in thread
From: Navid Emamdoost @ 2019-09-19 15:50 UTC (permalink / raw)
  To: alexandru.Ardelean
  Cc: emamd001, smccaman, kjlu, Navid Emamdoost, Lars-Peter Clausen,
	Michael Hennerich, Stefan Popa, Jonathan Cameron, Hartmut Knaack,
	Peter Meerwald-Stadler, linux-iio, linux-kernel

In adis_update_scan_mode, if allocation for adis->buffer fails,
previously allocated adis->xfer needs to be released.

v2: added adis->xfer = NULL to avoid any potential double free.

Signed-off-by: Navid Emamdoost <navid.emamdoost@gmail.com>
---
 drivers/iio/imu/adis_buffer.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/iio/imu/adis_buffer.c b/drivers/iio/imu/adis_buffer.c
index 9ac8356d9a95..f446ff497809 100644
--- a/drivers/iio/imu/adis_buffer.c
+++ b/drivers/iio/imu/adis_buffer.c
@@ -78,8 +78,11 @@ int adis_update_scan_mode(struct iio_dev *indio_dev,
 		return -ENOMEM;
 
 	adis->buffer = kcalloc(indio_dev->scan_bytes, 2, GFP_KERNEL);
-	if (!adis->buffer)
+	if (!adis->buffer) {
+		kfree(adis->xfer);
+		adis->xfer = NULL;
 		return -ENOMEM;
+	}
 
 	rx = adis->buffer;
 	tx = rx + scan_count;
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] iio: imu: adis16400: release allocated memory on failure
  2019-09-19 15:50   ` [PATCH v2] " Navid Emamdoost
@ 2019-09-20  6:46     ` Ardelean, Alexandru
  2019-09-21 18:11       ` Jonathan Cameron
  0 siblings, 1 reply; 5+ messages in thread
From: Ardelean, Alexandru @ 2019-09-20  6:46 UTC (permalink / raw)
  To: navid.emamdoost
  Cc: Popa, Stefan Serban, emamd001, linux-iio, linux-kernel, jic23,
	knaack.h, Hennerich, Michael, lars, smccaman, kjlu, pmeerw

On Thu, 2019-09-19 at 10:50 -0500, Navid Emamdoost wrote:
> In adis_update_scan_mode, if allocation for adis->buffer fails,
> previously allocated adis->xfer needs to be released.
> 
> v2: added adis->xfer = NULL to avoid any potential double free.

Reviewed-by: Alexandru Ardelean <alexandru.ardelean@analog.com>

> Signed-off-by: Navid Emamdoost <navid.emamdoost@gmail.com>
> ---
>  drivers/iio/imu/adis_buffer.c | 5 ++++-
>  1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/iio/imu/adis_buffer.c
> b/drivers/iio/imu/adis_buffer.c
> index 9ac8356d9a95..f446ff497809 100644
> --- a/drivers/iio/imu/adis_buffer.c
> +++ b/drivers/iio/imu/adis_buffer.c
> @@ -78,8 +78,11 @@ int adis_update_scan_mode(struct iio_dev *indio_dev,
>  		return -ENOMEM;
>  
>  	adis->buffer = kcalloc(indio_dev->scan_bytes, 2, GFP_KERNEL);
> -	if (!adis->buffer)
> +	if (!adis->buffer) {
> +		kfree(adis->xfer);
> +		adis->xfer = NULL;
>  		return -ENOMEM;
> +	}
>  
>  	rx = adis->buffer;
>  	tx = rx + scan_count;

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2] iio: imu: adis16400: release allocated memory on failure
  2019-09-20  6:46     ` Ardelean, Alexandru
@ 2019-09-21 18:11       ` Jonathan Cameron
  0 siblings, 0 replies; 5+ messages in thread
From: Jonathan Cameron @ 2019-09-21 18:11 UTC (permalink / raw)
  To: Ardelean, Alexandru
  Cc: navid.emamdoost, Popa, Stefan Serban, emamd001, linux-iio,
	linux-kernel, knaack.h, Hennerich, Michael, lars, smccaman, kjlu,
	pmeerw

On Fri, 20 Sep 2019 06:46:37 +0000
"Ardelean, Alexandru" <alexandru.Ardelean@analog.com> wrote:

> On Thu, 2019-09-19 at 10:50 -0500, Navid Emamdoost wrote:
> > In adis_update_scan_mode, if allocation for adis->buffer fails,
> > previously allocated adis->xfer needs to be released.
> > 
> > v2: added adis->xfer = NULL to avoid any potential double free. 

Version changes go below the --- so they don't get recorded in the
git tree log when it's applied.

 
> 
> Reviewed-by: Alexandru Ardelean <alexandru.ardelean@analog.com>
Applied to the fixes-togreg branch of iio.git. Thanks,

Jonathan

> 
> > Signed-off-by: Navid Emamdoost <navid.emamdoost@gmail.com>
> > ---
> >  drivers/iio/imu/adis_buffer.c | 5 ++++-
> >  1 file changed, 4 insertions(+), 1 deletion(-)
> > 
> > diff --git a/drivers/iio/imu/adis_buffer.c
> > b/drivers/iio/imu/adis_buffer.c
> > index 9ac8356d9a95..f446ff497809 100644
> > --- a/drivers/iio/imu/adis_buffer.c
> > +++ b/drivers/iio/imu/adis_buffer.c
> > @@ -78,8 +78,11 @@ int adis_update_scan_mode(struct iio_dev *indio_dev,
> >  		return -ENOMEM;
> >  
> >  	adis->buffer = kcalloc(indio_dev->scan_bytes, 2, GFP_KERNEL);
> > -	if (!adis->buffer)
> > +	if (!adis->buffer) {
> > +		kfree(adis->xfer);
> > +		adis->xfer = NULL;
> >  		return -ENOMEM;
> > +	}
> >  
> >  	rx = adis->buffer;
> >  	tx = rx + scan_count;  


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2019-09-21 18:11 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-09-18 16:57 [PATCH] iio: imu: adis16400: release allocated memory on failure Navid Emamdoost
2019-09-19  6:51 ` Ardelean, Alexandru
2019-09-19 15:50   ` [PATCH v2] " Navid Emamdoost
2019-09-20  6:46     ` Ardelean, Alexandru
2019-09-21 18:11       ` Jonathan Cameron

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).