[media] videobuf2: avoid memory leak on errors
diff mbox

Message ID b62ef37c6e2f30d1b5ce3889212050d738c04885.1450455268.git.mchehab@osg.samsung.com
State New
Headers show

Commit Message

Mauro Carvalho Chehab Dec. 18, 2015, 4:14 p.m. UTC
As reported by smatch:
	drivers/media/v4l2-core/videobuf2-core.c:2415 __vb2_init_fileio() warn: possible memory leak of 'fileio'

While here, avoid the usage of sizeof(struct foo_struct).

Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>
---
 drivers/media/v4l2-core/videobuf2-core.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

Comments

Marek Szyprowski Dec. 21, 2015, 6:54 a.m. UTC | #1
Hello,

On 2015-12-18 17:14, Mauro Carvalho Chehab wrote:
> As reported by smatch:
> 	drivers/media/v4l2-core/videobuf2-core.c:2415 __vb2_init_fileio() warn: possible memory leak of 'fileio'
>
> While here, avoid the usage of sizeof(struct foo_struct).
>
> Signed-off-by: Mauro Carvalho Chehab <mchehab@osg.samsung.com>

Acked-by: Marek Szyprowski <m.szyprowski@samsung.com>

> ---
>   drivers/media/v4l2-core/videobuf2-core.c | 6 ++++--
>   1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/media/v4l2-core/videobuf2-core.c b/drivers/media/v4l2-core/videobuf2-core.c
> index e6890d47cdcb..c5d49d7a0d76 100644
> --- a/drivers/media/v4l2-core/videobuf2-core.c
> +++ b/drivers/media/v4l2-core/videobuf2-core.c
> @@ -2406,13 +2406,15 @@ static int __vb2_init_fileio(struct vb2_queue *q, int read)
>   		(read) ? "read" : "write", count, q->fileio_read_once,
>   		q->fileio_write_immediately);
>   
> -	fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
> +	fileio = kzalloc(sizeof(*fileio), GFP_KERNEL);
>   	if (fileio == NULL)
>   		return -ENOMEM;
>   
>   	fileio->b = kzalloc(q->buf_struct_size, GFP_KERNEL);
> -	if (fileio->b == NULL)
> +	if (fileio->b == NULL) {
> +		kfree(fileio);
>   		return -ENOMEM;
> +	}
>   
>   	fileio->read_once = q->fileio_read_once;
>   	fileio->write_immediately = q->fileio_write_immediately;

Best regards

Patch
diff mbox

diff --git a/drivers/media/v4l2-core/videobuf2-core.c b/drivers/media/v4l2-core/videobuf2-core.c
index e6890d47cdcb..c5d49d7a0d76 100644
--- a/drivers/media/v4l2-core/videobuf2-core.c
+++ b/drivers/media/v4l2-core/videobuf2-core.c
@@ -2406,13 +2406,15 @@  static int __vb2_init_fileio(struct vb2_queue *q, int read)
 		(read) ? "read" : "write", count, q->fileio_read_once,
 		q->fileio_write_immediately);
 
-	fileio = kzalloc(sizeof(struct vb2_fileio_data), GFP_KERNEL);
+	fileio = kzalloc(sizeof(*fileio), GFP_KERNEL);
 	if (fileio == NULL)
 		return -ENOMEM;
 
 	fileio->b = kzalloc(q->buf_struct_size, GFP_KERNEL);
-	if (fileio->b == NULL)
+	if (fileio->b == NULL) {
+		kfree(fileio);
 		return -ENOMEM;
+	}
 
 	fileio->read_once = q->fileio_read_once;
 	fileio->write_immediately = q->fileio_write_immediately;