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

Message ID 20221122120428.25069-1-shangxiaojing@huawei.com
State New
Headers
Series comedi: drivers: pcl730: Fix potential memory leak in pcl730_attach() |

Commit Message

Shang XiaoJing Nov. 22, 2022, 12:04 p.m. UTC
  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()")
Signed-off-by: Shang XiaoJing <shangxiaojing@huawei.com>
---
 drivers/comedi/drivers/pcl730.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
  

Comments

Ian Abbott Nov. 22, 2022, 12:18 p.m. UTC | #1
On 22/11/2022 12:04, Shang XiaoJing wrote:
> 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()")
> Signed-off-by: Shang XiaoJing <shangxiaojing@huawei.com>
> ---
>   drivers/comedi/drivers/pcl730.c | 8 +++++++-
>   1 file changed, 7 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/comedi/drivers/pcl730.c b/drivers/comedi/drivers/pcl730.c
> index d2733cd5383d..c463de8a14e1 100644
> --- a/drivers/comedi/drivers/pcl730.c
> +++ b/drivers/comedi/drivers/pcl730.c
> @@ -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;
>   

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.)
  
Shang XiaoJing Nov. 22, 2022, 12:24 p.m. UTC | #2
On 2022/11/22 20:18, Ian Abbott wrote:
> On 22/11/2022 12:04, Shang XiaoJing wrote:
>> 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()")
>> Signed-off-by: Shang XiaoJing <shangxiaojing@huawei.com>
>> ---
>>   drivers/comedi/drivers/pcl730.c | 8 +++++++-
>>   1 file changed, 7 insertions(+), 1 deletion(-)
>>
>> diff --git a/drivers/comedi/drivers/pcl730.c 
>> b/drivers/comedi/drivers/pcl730.c
>> index d2733cd5383d..c463de8a14e1 100644
>> --- a/drivers/comedi/drivers/pcl730.c
>> +++ b/drivers/comedi/drivers/pcl730.c
>> @@ -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;
> 
> 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,
  

Patch

diff --git a/drivers/comedi/drivers/pcl730.c b/drivers/comedi/drivers/pcl730.c
index d2733cd5383d..c463de8a14e1 100644
--- a/drivers/comedi/drivers/pcl730.c
+++ b/drivers/comedi/drivers/pcl730.c
@@ -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;