diff mbox

[1/3,media] au0828-video: Use kcalloc() in au0828_init_isoc()

Message ID 68ad1aaa-c029-04b9-805a-e859f6c2d2d5@users.sourceforge.net (mailing list archive)
State New, archived
Headers show

Commit Message

SF Markus Elfring Oct. 24, 2016, 8:59 p.m. UTC
From: Markus Elfring <elfring@users.sourceforge.net>
Date: Mon, 24 Oct 2016 22:08:47 +0200

* Multiplications for the size determination of memory allocations
  indicated that array data structures should be processed.
  Thus use the corresponding function "kcalloc".

  This issue was detected by using the Coccinelle software.

* Replace the specification of data types by pointer dereferences
  to make the corresponding size determination a bit safer according to
  the Linux coding style convention.

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---
 drivers/media/usb/au0828/au0828-video.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

Comments

Andrey Utkin Oct. 24, 2016, 10:28 p.m. UTC | #1
On Mon, Oct 24, 2016 at 10:59:24PM +0200, SF Markus Elfring wrote:
> From: Markus Elfring <elfring@users.sourceforge.net>
> Date: Mon, 24 Oct 2016 22:08:47 +0200
> 
> * Multiplications for the size determination of memory allocations
>   indicated that array data structures should be processed.
>   Thus use the corresponding function "kcalloc".
> 
>   This issue was detected by using the Coccinelle software.
> 
> * Replace the specification of data types by pointer dereferences
>   to make the corresponding size determination a bit safer according to
>   the Linux coding style convention.
> 
> Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
> ---
>  drivers/media/usb/au0828/au0828-video.c | 11 +++++++----
>  1 file changed, 7 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/media/usb/au0828/au0828-video.c b/drivers/media/usb/au0828/au0828-video.c
> index 85dd9a8..85b13c1 100644
> --- a/drivers/media/usb/au0828/au0828-video.c
> +++ b/drivers/media/usb/au0828/au0828-video.c
> @@ -221,15 +221,18 @@ static int au0828_init_isoc(struct au0828_dev *dev, int max_packets,
>  
>  	dev->isoc_ctl.isoc_copy = isoc_copy;
>  	dev->isoc_ctl.num_bufs = num_bufs;
> -

> -	dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs,  GFP_KERNEL);
> +	dev->isoc_ctl.urb = kcalloc(num_bufs,
> +				    sizeof(*dev->isoc_ctl.urb),
> +				    GFP_KERNEL);

What about this (for both hunks)?

-	dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs,  GFP_KERNEL);
+	dev->isoc_ctl.urb =
+		kcalloc(num_bufs, sizeof(*dev->isoc_ctl.urb), GFP_KERNEL);
--
To unsubscribe from this list: send the line "unsubscribe linux-media" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Mauro Carvalho Chehab Oct. 25, 2016, 12:11 a.m. UTC | #2
Em Mon, 24 Oct 2016 23:28:44 +0100
Andrey Utkin <andrey_utkin@fastmail.com> escreveu:

> On Mon, Oct 24, 2016 at 10:59:24PM +0200, SF Markus Elfring wrote:
> > From: Markus Elfring <elfring@users.sourceforge.net>
> > Date: Mon, 24 Oct 2016 22:08:47 +0200
> > 
> > * Multiplications for the size determination of memory allocations
> >   indicated that array data structures should be processed.
> >   Thus use the corresponding function "kcalloc".
> > 
> >   This issue was detected by using the Coccinelle software.
> > 
> > * Replace the specification of data types by pointer dereferences
> >   to make the corresponding size determination a bit safer according to
> >   the Linux coding style convention.
> > 
> > Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
> > ---
> >  drivers/media/usb/au0828/au0828-video.c | 11 +++++++----
> >  1 file changed, 7 insertions(+), 4 deletions(-)
> > 
> > diff --git a/drivers/media/usb/au0828/au0828-video.c b/drivers/media/usb/au0828/au0828-video.c
> > index 85dd9a8..85b13c1 100644
> > --- a/drivers/media/usb/au0828/au0828-video.c
> > +++ b/drivers/media/usb/au0828/au0828-video.c
> > @@ -221,15 +221,18 @@ static int au0828_init_isoc(struct au0828_dev *dev, int max_packets,
> >  
> >  	dev->isoc_ctl.isoc_copy = isoc_copy;
> >  	dev->isoc_ctl.num_bufs = num_bufs;
> > -  
> 
> > -	dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs,  GFP_KERNEL);
> > +	dev->isoc_ctl.urb = kcalloc(num_bufs,
> > +				    sizeof(*dev->isoc_ctl.urb),
> > +				    GFP_KERNEL);  
> 
> What about this (for both hunks)?
> 
> -	dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs,  GFP_KERNEL);
> +	dev->isoc_ctl.urb =
> +		kcalloc(num_bufs, sizeof(*dev->isoc_ctl.urb), GFP_KERNEL);


That's worse :)

The usual Kernel style is:

		var = foo(bar1,
		          bar2,
		          bar3);

instead of something like:

		var =
		    foo(bar1,
			bar2,
			bar3);

The places where it is different than that is because people ran
./scripts/Lindent to try to follow the Kernel coding style.

On my experiences, at the end, using it caused more harm than
good, IMHO, and cause very weird indentation on lines with
more than 80 columns like the above.

Thanks,
Mauro
--
To unsubscribe from this list: send the line "unsubscribe linux-media" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Julia Lawall Oct. 25, 2016, 5:51 a.m. UTC | #3
On Mon, 24 Oct 2016, Mauro Carvalho Chehab wrote:

> Em Mon, 24 Oct 2016 23:28:44 +0100
> Andrey Utkin <andrey_utkin@fastmail.com> escreveu:
>
> > On Mon, Oct 24, 2016 at 10:59:24PM +0200, SF Markus Elfring wrote:
> > > From: Markus Elfring <elfring@users.sourceforge.net>
> > > Date: Mon, 24 Oct 2016 22:08:47 +0200
> > >
> > > * Multiplications for the size determination of memory allocations
> > >   indicated that array data structures should be processed.
> > >   Thus use the corresponding function "kcalloc".
> > >
> > >   This issue was detected by using the Coccinelle software.
> > >
> > > * Replace the specification of data types by pointer dereferences
> > >   to make the corresponding size determination a bit safer according to
> > >   the Linux coding style convention.
> > >
> > > Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
> > > ---
> > >  drivers/media/usb/au0828/au0828-video.c | 11 +++++++----
> > >  1 file changed, 7 insertions(+), 4 deletions(-)
> > >
> > > diff --git a/drivers/media/usb/au0828/au0828-video.c b/drivers/media/usb/au0828/au0828-video.c
> > > index 85dd9a8..85b13c1 100644
> > > --- a/drivers/media/usb/au0828/au0828-video.c
> > > +++ b/drivers/media/usb/au0828/au0828-video.c
> > > @@ -221,15 +221,18 @@ static int au0828_init_isoc(struct au0828_dev *dev, int max_packets,
> > >
> > >  	dev->isoc_ctl.isoc_copy = isoc_copy;
> > >  	dev->isoc_ctl.num_bufs = num_bufs;
> > > -
> >
> > > -	dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs,  GFP_KERNEL);
> > > +	dev->isoc_ctl.urb = kcalloc(num_bufs,
> > > +				    sizeof(*dev->isoc_ctl.urb),
> > > +				    GFP_KERNEL);
> >
> > What about this (for both hunks)?
> >
> > -	dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs,  GFP_KERNEL);
> > +	dev->isoc_ctl.urb =
> > +		kcalloc(num_bufs, sizeof(*dev->isoc_ctl.urb), GFP_KERNEL);
>
>
> That's worse :)
>
> The usual Kernel style is:
>
> 		var = foo(bar1,
> 		          bar2,
> 		          bar3);

Isn't it more like

var = foo(bar1, bar2,
          bar3)

Otherwise, Markus is going to send millions of patches to put every
function argument on its own line...

julia

>
> instead of something like:
>
> 		var =
> 		    foo(bar1,
> 			bar2,
> 			bar3);
>
> The places where it is different than that is because people ran
> ./scripts/Lindent to try to follow the Kernel coding style.
>
> On my experiences, at the end, using it caused more harm than
> good, IMHO, and cause very weird indentation on lines with
> more than 80 columns like the above.
>
> Thanks,
> Mauro
> --
> To unsubscribe from this list: send the line "unsubscribe kernel-janitors" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>
--
To unsubscribe from this list: send the line "unsubscribe linux-media" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Dan Carpenter Oct. 25, 2016, 8:40 a.m. UTC | #4
On Mon, Oct 24, 2016 at 10:59:24PM +0200, SF Markus Elfring wrote:
> +	dev->isoc_ctl.transfer_buffer = kcalloc(num_bufs,
> +						sizeof(*dev->isoc_ctl
> +						       .transfer_buffer),

This is ugly.

regards,
dan carpenter

--
To unsubscribe from this list: send the line "unsubscribe linux-media" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Andrey Utkin Oct. 25, 2016, 12:46 p.m. UTC | #5
On Mon, Oct 24, 2016 at 10:11:15PM -0200, Mauro Carvalho Chehab wrote:
> Em Mon, 24 Oct 2016 23:28:44 +0100
> Andrey Utkin <andrey_utkin@fastmail.com> escreveu:
> 
> > On Mon, Oct 24, 2016 at 10:59:24PM +0200, SF Markus Elfring wrote:
> > > From: Markus Elfring <elfring@users.sourceforge.net>
> > > Date: Mon, 24 Oct 2016 22:08:47 +0200
> > > 
> > > * Multiplications for the size determination of memory allocations
> > >   indicated that array data structures should be processed.
> > >   Thus use the corresponding function "kcalloc".
> > > 
> > >   This issue was detected by using the Coccinelle software.
> > > 
> > > * Replace the specification of data types by pointer dereferences
> > >   to make the corresponding size determination a bit safer according to
> > >   the Linux coding style convention.
> > > 
> > > Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
> > > ---
> > >  drivers/media/usb/au0828/au0828-video.c | 11 +++++++----
> > >  1 file changed, 7 insertions(+), 4 deletions(-)
> > > 
> > > diff --git a/drivers/media/usb/au0828/au0828-video.c b/drivers/media/usb/au0828/au0828-video.c
> > > index 85dd9a8..85b13c1 100644
> > > --- a/drivers/media/usb/au0828/au0828-video.c
> > > +++ b/drivers/media/usb/au0828/au0828-video.c
> > > @@ -221,15 +221,18 @@ static int au0828_init_isoc(struct au0828_dev *dev, int max_packets,
> > >  
> > >  	dev->isoc_ctl.isoc_copy = isoc_copy;
> > >  	dev->isoc_ctl.num_bufs = num_bufs;
> > > -  
> > 
> > > -	dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs,  GFP_KERNEL);
> > > +	dev->isoc_ctl.urb = kcalloc(num_bufs,
> > > +				    sizeof(*dev->isoc_ctl.urb),
> > > +				    GFP_KERNEL);  
> > 
> > What about this (for both hunks)?
> > 
> > -	dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs,  GFP_KERNEL);
> > +	dev->isoc_ctl.urb =
> > +		kcalloc(num_bufs, sizeof(*dev->isoc_ctl.urb), GFP_KERNEL);

Now i see that also this should suit style better than original variant:

	dev->isoc_ctl.urb = kcalloc(num_bufs, sizeof(*dev->isoc_ctl.urb),
				    GFP_KERNEL);

That's what vim with github.com/vivien/vim-linux-coding-style plugin
proposes.

> That's worse :)

I was about to send long emotional noobish bikeshedding rant arguing
with this point, but restrained from that keeping in mind that I want to
proceed contributing to the codebase successfully :) I'll keep my coding
style preferences for myself for a while.
--
To unsubscribe from this list: send the line "unsubscribe linux-media" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/drivers/media/usb/au0828/au0828-video.c b/drivers/media/usb/au0828/au0828-video.c
index 85dd9a8..85b13c1 100644
--- a/drivers/media/usb/au0828/au0828-video.c
+++ b/drivers/media/usb/au0828/au0828-video.c
@@ -221,15 +221,18 @@  static int au0828_init_isoc(struct au0828_dev *dev, int max_packets,
 
 	dev->isoc_ctl.isoc_copy = isoc_copy;
 	dev->isoc_ctl.num_bufs = num_bufs;
-
-	dev->isoc_ctl.urb = kzalloc(sizeof(void *)*num_bufs,  GFP_KERNEL);
+	dev->isoc_ctl.urb = kcalloc(num_bufs,
+				    sizeof(*dev->isoc_ctl.urb),
+				    GFP_KERNEL);
 	if (!dev->isoc_ctl.urb) {
 		au0828_isocdbg("cannot alloc memory for usb buffers\n");
 		return -ENOMEM;
 	}
 
-	dev->isoc_ctl.transfer_buffer = kzalloc(sizeof(void *)*num_bufs,
-					      GFP_KERNEL);
+	dev->isoc_ctl.transfer_buffer = kcalloc(num_bufs,
+						sizeof(*dev->isoc_ctl
+						       .transfer_buffer),
+						GFP_KERNEL);
 	if (!dev->isoc_ctl.transfer_buffer) {
 		au0828_isocdbg("cannot allocate memory for usb transfer\n");
 		kfree(dev->isoc_ctl.urb);