Skip to contentSkip to conversation

iio: imu: adis16400: release allocated memory on failure

PATCH Superseded Navid Emamdoost 3 participants 5 messages Sep 18, 2019 – Sep 21, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iio: imu: adis16400: release allocated memory on failure

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

---
 drivers/iio/imu/adis_buffer.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/iio/imu/adis_buffer.c +3 -1
index 9ac8356d9a95..c5d7e368a636 100644
@@ -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;
Signature · 1 line
2.17.1
#1 lore raw
Quoted 2 lines · Navid Emamdoost

[External]

Hey,

Good catch.
One comment inline.

Quoted 14 lines

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

---
 drivers/iio/imu/adis_buffer.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/iio/imu/adis_buffer.c +0 -0
index 9ac8356d9a95..c5d7e368a636 100644

drivers/iio/imu/adis_buffer.c int adis_update_scan_mode(struct iio_dev *indio_dev, · line 82

Quoted 6 lines
drivers/iio/imu/adis_buffer.c +2 -1
@@ -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).

Quoted 5 lines
drivers/iio/imu/adis_buffer.c +1 -0
 		return -ENOMEM;
+	}
 
 	rx = adis->buffer;
 	tx = rx + scan_count;
#2 lore raw

PATCH v2 1/1 iio: imu: adis16400: release allocated memory on failure

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.

---
 drivers/iio/imu/adis_buffer.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/iio/imu/adis_buffer.c +4 -1
index 9ac8356d9a95..f446ff497809 100644
@@ -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;
Signature · 1 line
2.17.1
Reviewed-by on v2 1/1
#3 lore raw
Quoted 4 lines · Navid Emamdoost

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.

Quoted 23 lines
---
 drivers/iio/imu/adis_buffer.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/iio/imu/adis_buffer.c +4 -1
index 9ac8356d9a95..f446ff497809 100644
@@ -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;
maintainer Applied
#4 lore raw
Quoted 5 lines · Alexandru
Quoted 4 lines · Navid Emamdoost

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.

Quoted 2 lines

Applied to the fixes-togreg branch of iio.git. Thanks,

Jonathan

Quoted 24 lines
Quoted 24 lines
---
 drivers/iio/imu/adis_buffer.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/iio/imu/adis_buffer.c +4 -1
index 9ac8356d9a95..f446ff497809 100644
@@ -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;