Skip to contentSkip to conversation

comedi: drivers: pcl730: Fix potential memory leak in pcl730_attach()

PATCH In review Shang XiaoJing 2 participants 3 messages Nov 22, 2022 Comedi drivers

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 comedi: drivers: pcl730: Fix potential memory leak in pcl730_attach()

pcl730_attach() calls comedi_request_region() and won't release the resource allocated by alloc_resource() when pcl730_attach() failed latter. Add release_region() to prevent memory leak.

Fixes 6f9aa29b47f6 ("staging: comedi: pcl730: use comedi_request_region()")
---
 drivers/comedi/drivers/pcl730.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/comedi/drivers/pcl730.c +7 -1
index d2733cd5383d..c463de8a14e1 100644
@@ -274,8 +274,14 @@ static int pcl730_attach(struct comedi_device *dev,
 		return ret;
 
 	ret = comedi_alloc_subdevices(dev, board->n_subdevs);
-	if (ret)
+	if (ret) {
+		if (dev->iobase && dev->iolen) {
+			release_region(dev->iobase, dev->iolen);
+			dev->iobase = 0;
+			dev->iolen = 0;
+		}
 		return ret;
+	}
 
 	subdev = 0;
 
Signature · 1 line
2.17.1
maintainer
#1 lore raw
Quoted 20 lines · Shang XiaoJing

pcl730_attach() calls comedi_request_region() and won't release the resource allocated by alloc_resource() when pcl730_attach() failed latter. Add release_region() to prevent memory leak.

Fixes 6f9aa29b47f6 ("staging: comedi: pcl730: use comedi_request_region()")
---
  drivers/comedi/drivers/pcl730.c | 8 +++++++-
  1 file changed, 7 insertions(+), 1 deletion(-)
drivers/comedi/drivers/pcl730.c +1 -1
index d2733cd5383d..c463de8a14e1 100644
@@ -274,8 +274,14 @@ static int pcl730_attach(struct comedi_device *dev,
  		return ret;
 
  	ret = comedi_alloc_subdevices(dev, board->n_subdevs);
-	if (ret)
+	if (ret) {

drivers/comedi/drivers/pcl730.c static int pcl730_attach(struct comedi_device *dev, · line 287

Quoted 10 lines
drivers/comedi/drivers/pcl730.c +6 -0
+		if (dev->iobase && dev->iolen) {
+			release_region(dev->iobase, dev->iolen);
+			dev->iobase = 0;
+			dev->iolen = 0;
+		}
  		return ret;
+	}
 
  	subdev = 0;
 

This is not needed. If the 'attach' handler pcl730_attach() returns an error, the 'detach' handler comedi_legacy_detach() will be called to clean up the allocated resources. All the comedi drivers work that way. (A lot of them have an 'auto_attach' handler instead of an 'attach' handler, but the error handling is basically the same.)

Signature · 4 lines
-=( Ian Abbott <[email protected]> || MEV Ltd. is a company  )=-
-=( registered in England & Wales.  Regd. number: 02862268.  )=-
-=( Regd. addr.: S11 & 12 Building 67, Europa Business Park, )=-
-=( Bird Hall Lane, STOCKPORT, SK3 0XA, UK. || www.mev.co.uk )=-
#2 lore raw
Quoted 22 lines · Ian Abbott
Quoted 21 lines · Shang XiaoJing

pcl730_attach() calls comedi_request_region() and won't release the resource allocated by alloc_resource() when pcl730_attach() failed latter. Add release_region() to prevent memory leak.

Fixes 6f9aa29b47f6 ("staging: comedi: pcl730: use comedi_request_region()")

---
  drivers/comedi/drivers/pcl730.c | 8 +++++++-

  1 file changed, 7 insertions(+), 1 deletion(-)
drivers/comedi/drivers/pcl730.c +3 -1
index d2733cd5383d..c463de8a14e1 100644
@@ -274,8 +274,14 @@ static int pcl730_attach(struct comedi_device *dev,
           return ret;
       ret = comedi_alloc_subdevices(dev, board->n_subdevs);
-    if (ret)
+    if (ret) {
+        if (dev->iobase && dev->iolen) {
+            release_region(dev->iobase, dev->iolen);
Quoted 9 lines

drivers/comedi/drivers/pcl730.c static int pcl730_attach(struct comedi_device *dev, · line 286

Quoted 6 lines
drivers/comedi/drivers/pcl730.c +4 -0
+            dev->iobase = 0;
+            dev->iolen = 0;
+        }
           return ret;
+    }
       subdev = 0;

This is not needed.  If the 'attach' handler pcl730_attach() returns an error, the 'detach' handler comedi_legacy_detach() will be called to clean up the allocated resources.  All the comedi drivers work that way. (A lot of them have an 'auto_attach' handler instead of an 'attach' handler, but the error handling is basically the same.)

ok, thanks for the advice!

Thanks,

Signature · 1 line
Shang XiaoJing