diff mbox

[01/23] uvc: Replace memcpy with struct assignment

Message ID 1351022246-8201-1-git-send-email-elezegarcia@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Ezequiel Garcia Oct. 23, 2012, 7:57 p.m. UTC
This kind of memcpy() is error-prone. Its replacement with a struct
assignment is prefered because it's type-safe and much easier to read.

Found by coccinelle. Hand patched and reviewed.
Tested by compilation only.

A simplified version of the semantic match that finds this problem is as
follows: (http://coccinelle.lip6.fr/)

// <smpl>
@@
identifier struct_name;
struct struct_name to;
struct struct_name from;
expression E;
@@
-memcpy(&(to), &(from), E);
+to = from;
// </smpl>

Cc: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Peter Senna Tschudin <peter.senna@gmail.com>
Signed-off-by: Ezequiel Garcia <elezegarcia@gmail.com>
---
 drivers/media/usb/uvc/uvc_v4l2.c |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)

Comments

Laurent Pinchart Oct. 24, 2012, 10:35 p.m. UTC | #1
Hi Ezequiel,

Thanks for the patch.

On Tuesday 23 October 2012 16:57:04 Ezequiel Garcia wrote:
> This kind of memcpy() is error-prone. Its replacement with a struct
> assignment is prefered because it's type-safe and much easier to read.
> 
> Found by coccinelle. Hand patched and reviewed.
> Tested by compilation only.

This looks good, but there's one more memcpy that can be replaced by a direct 
structure assignment in uvc_ctrl_add_info() 
(drivers/media/usb/uvc/uvc_ctrl.c). You might want to check why it hasn't been 
caught by the semantic patch.

> A simplified version of the semantic match that finds this problem is as
> follows: (http://coccinelle.lip6.fr/)
> 
> // <smpl>
> @@
> identifier struct_name;
> struct struct_name to;
> struct struct_name from;
> expression E;
> @@
> -memcpy(&(to), &(from), E);
> +to = from;
> // </smpl>
> 
> Cc: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> Signed-off-by: Peter Senna Tschudin <peter.senna@gmail.com>
> Signed-off-by: Ezequiel Garcia <elezegarcia@gmail.com>
> ---
>  drivers/media/usb/uvc/uvc_v4l2.c |    6 +++---
>  1 files changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/media/usb/uvc/uvc_v4l2.c
> b/drivers/media/usb/uvc/uvc_v4l2.c index f00db30..4fc8737 100644
> --- a/drivers/media/usb/uvc/uvc_v4l2.c
> +++ b/drivers/media/usb/uvc/uvc_v4l2.c
> @@ -314,7 +314,7 @@ static int uvc_v4l2_set_format(struct uvc_streaming
> *stream, goto done;
>  	}
> 
> -	memcpy(&stream->ctrl, &probe, sizeof probe);
> +	stream->ctrl = probe;
>  	stream->cur_format = format;
>  	stream->cur_frame = frame;
> 
> @@ -386,7 +386,7 @@ static int uvc_v4l2_set_streamparm(struct uvc_streaming
> *stream, return -EBUSY;
>  	}
> 
> -	memcpy(&probe, &stream->ctrl, sizeof probe);
> +	probe = stream->ctrl;
>  	probe.dwFrameInterval =
>  		uvc_try_frame_interval(stream->cur_frame, interval);
> 
> @@ -397,7 +397,7 @@ static int uvc_v4l2_set_streamparm(struct uvc_streaming
> *stream, return ret;
>  	}
> 
> -	memcpy(&stream->ctrl, &probe, sizeof probe);
> +	stream->ctrl = probe;
>  	mutex_unlock(&stream->mutex);
> 
>  	/* Return the actual frame period. */
Andy Walls Oct. 24, 2012, 11:10 p.m. UTC | #2
Laurent Pinchart <laurent.pinchart@ideasonboard.com> wrote:

>Hi Ezequiel,
>
>Thanks for the patch.
>
>On Tuesday 23 October 2012 16:57:04 Ezequiel Garcia wrote:
>> This kind of memcpy() is error-prone. Its replacement with a struct
>> assignment is prefered because it's type-safe and much easier to
>read.
>> 
>> Found by coccinelle. Hand patched and reviewed.
>> Tested by compilation only.
>
>This looks good, but there's one more memcpy that can be replaced by a
>direct 
>structure assignment in uvc_ctrl_add_info() 
>(drivers/media/usb/uvc/uvc_ctrl.c). You might want to check why it
>hasn't been 
>caught by the semantic patch.
>
>> A simplified version of the semantic match that finds this problem is
>as
>> follows: (http://coccinelle.lip6.fr/)
>> 
>> // <smpl>
>> @@
>> identifier struct_name;
>> struct struct_name to;
>> struct struct_name from;
>> expression E;
>> @@
>> -memcpy(&(to), &(from), E);
>> +to = from;
>> // </smpl>
>> 
>> Cc: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
>> Signed-off-by: Peter Senna Tschudin <peter.senna@gmail.com>
>> Signed-off-by: Ezequiel Garcia <elezegarcia@gmail.com>
>> ---
>>  drivers/media/usb/uvc/uvc_v4l2.c |    6 +++---
>>  1 files changed, 3 insertions(+), 3 deletions(-)
>> 
>> diff --git a/drivers/media/usb/uvc/uvc_v4l2.c
>> b/drivers/media/usb/uvc/uvc_v4l2.c index f00db30..4fc8737 100644
>> --- a/drivers/media/usb/uvc/uvc_v4l2.c
>> +++ b/drivers/media/usb/uvc/uvc_v4l2.c
>> @@ -314,7 +314,7 @@ static int uvc_v4l2_set_format(struct
>uvc_streaming
>> *stream, goto done;
>>  	}
>> 
>> -	memcpy(&stream->ctrl, &probe, sizeof probe);
>> +	stream->ctrl = probe;
>>  	stream->cur_format = format;
>>  	stream->cur_frame = frame;
>> 
>> @@ -386,7 +386,7 @@ static int uvc_v4l2_set_streamparm(struct
>uvc_streaming
>> *stream, return -EBUSY;
>>  	}
>> 
>> -	memcpy(&probe, &stream->ctrl, sizeof probe);
>> +	probe = stream->ctrl;
>>  	probe.dwFrameInterval =
>>  		uvc_try_frame_interval(stream->cur_frame, interval);
>> 
>> @@ -397,7 +397,7 @@ static int uvc_v4l2_set_streamparm(struct
>uvc_streaming
>> *stream, return ret;
>>  	}
>> 
>> -	memcpy(&stream->ctrl, &probe, sizeof probe);
>> +	stream->ctrl = probe;
>>  	mutex_unlock(&stream->mutex);
>> 
>>  	/* Return the actual frame period. */
>
>-- 
>Regards,
>
>Laurent Pinchart
>
>--
>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

Maybe because there is no '&' operator on the second argument. 

Regards,
Andy
--
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
Ezequiel Garcia Dec. 27, 2012, 9:12 p.m. UTC | #3
Mauro,

On Tue, Oct 23, 2012 at 4:57 PM, Ezequiel Garcia <elezegarcia@gmail.com> wrote:
> This kind of memcpy() is error-prone. Its replacement with a struct
> assignment is prefered because it's type-safe and much easier to read.
>
> Found by coccinelle. Hand patched and reviewed.
> Tested by compilation only.
>
> A simplified version of the semantic match that finds this problem is as
> follows: (http://coccinelle.lip6.fr/)
>
> // <smpl>
> @@
> identifier struct_name;
> struct struct_name to;
> struct struct_name from;
> expression E;
> @@
> -memcpy(&(to), &(from), E);
> +to = from;
> // </smpl>
>
> Cc: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> Signed-off-by: Peter Senna Tschudin <peter.senna@gmail.com>
> Signed-off-by: Ezequiel Garcia <elezegarcia@gmail.com>
> ---
>  drivers/media/usb/uvc/uvc_v4l2.c |    6 +++---
>  1 files changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c
> index f00db30..4fc8737 100644
> --- a/drivers/media/usb/uvc/uvc_v4l2.c
> +++ b/drivers/media/usb/uvc/uvc_v4l2.c
> @@ -314,7 +314,7 @@ static int uvc_v4l2_set_format(struct uvc_streaming *stream,
>                 goto done;
>         }
>
> -       memcpy(&stream->ctrl, &probe, sizeof probe);
> +       stream->ctrl = probe;
>         stream->cur_format = format;
>         stream->cur_frame = frame;
>
> @@ -386,7 +386,7 @@ static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
>                 return -EBUSY;
>         }
>
> -       memcpy(&probe, &stream->ctrl, sizeof probe);
> +       probe = stream->ctrl;
>         probe.dwFrameInterval =
>                 uvc_try_frame_interval(stream->cur_frame, interval);
>
> @@ -397,7 +397,7 @@ static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
>                 return ret;
>         }
>
> -       memcpy(&stream->ctrl, &probe, sizeof probe);
> +       stream->ctrl = probe;
>         mutex_unlock(&stream->mutex);
>
>         /* Return the actual frame period. */
> --
> 1.7.4.4
>

It seems you've marked this one as "Changes requested" [1].
However, Laurent didn't request any change,
but just pointed out we missed one memcpy replacement candidate.

I believe it's safe to apply the patch (together with the other 20 patches)
and we can fix the missing spot in another patch.

Thanks,
Mauro Carvalho Chehab Dec. 27, 2012, 11:49 p.m. UTC | #4
Em Thu, 27 Dec 2012 18:12:46 -0300
Ezequiel Garcia <elezegarcia@gmail.com> escreveu:

> Mauro,
> 
> On Tue, Oct 23, 2012 at 4:57 PM, Ezequiel Garcia <elezegarcia@gmail.com> wrote:
> > This kind of memcpy() is error-prone. Its replacement with a struct
> > assignment is prefered because it's type-safe and much easier to read.
> >
> > Found by coccinelle. Hand patched and reviewed.
> > Tested by compilation only.
> >
> > A simplified version of the semantic match that finds this problem is as
> > follows: (http://coccinelle.lip6.fr/)
> >
> > // <smpl>
> > @@
> > identifier struct_name;
> > struct struct_name to;
> > struct struct_name from;
> > expression E;
> > @@
> > -memcpy(&(to), &(from), E);
> > +to = from;
> > // </smpl>
> >
> > Cc: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> > Signed-off-by: Peter Senna Tschudin <peter.senna@gmail.com>
> > Signed-off-by: Ezequiel Garcia <elezegarcia@gmail.com>
> > ---
> >  drivers/media/usb/uvc/uvc_v4l2.c |    6 +++---
> >  1 files changed, 3 insertions(+), 3 deletions(-)
> >
> > diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c
> > index f00db30..4fc8737 100644
> > --- a/drivers/media/usb/uvc/uvc_v4l2.c
> > +++ b/drivers/media/usb/uvc/uvc_v4l2.c
> > @@ -314,7 +314,7 @@ static int uvc_v4l2_set_format(struct uvc_streaming *stream,
> >                 goto done;
> >         }
> >
> > -       memcpy(&stream->ctrl, &probe, sizeof probe);
> > +       stream->ctrl = probe;
> >         stream->cur_format = format;
> >         stream->cur_frame = frame;
> >
> > @@ -386,7 +386,7 @@ static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
> >                 return -EBUSY;
> >         }
> >
> > -       memcpy(&probe, &stream->ctrl, sizeof probe);
> > +       probe = stream->ctrl;
> >         probe.dwFrameInterval =
> >                 uvc_try_frame_interval(stream->cur_frame, interval);
> >
> > @@ -397,7 +397,7 @@ static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
> >                 return ret;
> >         }
> >
> > -       memcpy(&stream->ctrl, &probe, sizeof probe);
> > +       stream->ctrl = probe;
> >         mutex_unlock(&stream->mutex);
> >
> >         /* Return the actual frame period. */
> > --
> > 1.7.4.4
> >
> 
> It seems you've marked this one as "Changes requested" [1].
> However, Laurent didn't request any change,
> but just pointed out we missed one memcpy replacement candidate.
> 
> I believe it's safe to apply the patch (together with the other 20 patches)
> and we can fix the missing spot in another patch.

The other patches got applied already. Well just do whatever Laurent asked you
and re-submit this one ;)

Regards,
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
Laurent Pinchart Jan. 10, 2013, 12:19 a.m. UTC | #5
Hi Ezequiel,

On Thursday 27 December 2012 21:49:37 Mauro Carvalho Chehab wrote:
> Em Thu, 27 Dec 2012 18:12:46 -0300 Ezequiel Garcia escreveu:
> > On Tue, Oct 23, 2012 at 4:57 PM, Ezequiel Garcia wrote:
> > > This kind of memcpy() is error-prone. Its replacement with a struct
> > > assignment is prefered because it's type-safe and much easier to read.
> > > 
> > > Found by coccinelle. Hand patched and reviewed.
> > > Tested by compilation only.
> > > 
> > > A simplified version of the semantic match that finds this problem is as
> > > follows: (http://coccinelle.lip6.fr/)
> > > 
> > > // <smpl>
> > > @@
> > > identifier struct_name;
> > > struct struct_name to;
> > > struct struct_name from;
> > > expression E;
> > > @@
> > > -memcpy(&(to), &(from), E);
> > > +to = from;
> > > // </smpl>
> > > 
> > > Cc: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> > > Signed-off-by: Peter Senna Tschudin <peter.senna@gmail.com>
> > > Signed-off-by: Ezequiel Garcia <elezegarcia@gmail.com>
> > > ---
> > > 
> > >  drivers/media/usb/uvc/uvc_v4l2.c |    6 +++---
> > >  1 files changed, 3 insertions(+), 3 deletions(-)
> > > 
> > > diff --git a/drivers/media/usb/uvc/uvc_v4l2.c
> > > b/drivers/media/usb/uvc/uvc_v4l2.c index f00db30..4fc8737 100644
> > > --- a/drivers/media/usb/uvc/uvc_v4l2.c
> > > +++ b/drivers/media/usb/uvc/uvc_v4l2.c
> > > @@ -314,7 +314,7 @@ static int uvc_v4l2_set_format(struct uvc_streaming
> > > *stream,
> > >                 goto done;
> > >         }
> > > 
> > > -       memcpy(&stream->ctrl, &probe, sizeof probe);
> > > +       stream->ctrl = probe;
> > > 
> > >         stream->cur_format = format;
> > >         stream->cur_frame = frame;
> > > @@ -386,7 +386,7 @@ static int uvc_v4l2_set_streamparm(struct
> > > uvc_streaming *stream,
> > >                 return -EBUSY;
> > >         }
> > > 
> > > -       memcpy(&probe, &stream->ctrl, sizeof probe);
> > > +       probe = stream->ctrl;
> > >         probe.dwFrameInterval =
> > >                 uvc_try_frame_interval(stream->cur_frame, interval);
> > > 
> > > @@ -397,7 +397,7 @@ static int uvc_v4l2_set_streamparm(struct
> > > uvc_streaming *stream,
> > >                 return ret;
> > >         }
> > > 
> > > -       memcpy(&stream->ctrl, &probe, sizeof probe);
> > > +       stream->ctrl = probe;
> > >         mutex_unlock(&stream->mutex);
> > >         
> > >         /* Return the actual frame period. */
> > 
> > It seems you've marked this one as "Changes requested" [1].
> > However, Laurent didn't request any change,
> > but just pointed out we missed one memcpy replacement candidate.
> > 
> > I believe it's safe to apply the patch (together with the other 20
> > patches) and we can fix the missing spot in another patch.
> 
> The other patches got applied already. Well just do whatever Laurent asked
> you and re-submit this one ;)

Could you please resubmit this patch with the missed memcpy replaced by a 
struct assignment ? I'll then add it to my tree.
Ezequiel Garcia Jan. 12, 2013, 5:44 p.m. UTC | #6
On Wed, Jan 9, 2013 at 9:19 PM, Laurent Pinchart
<laurent.pinchart@ideasonboard.com> wrote:
> Hi Ezequiel,
>
> On Thursday 27 December 2012 21:49:37 Mauro Carvalho Chehab wrote:
>> Em Thu, 27 Dec 2012 18:12:46 -0300 Ezequiel Garcia escreveu:
>> > On Tue, Oct 23, 2012 at 4:57 PM, Ezequiel Garcia wrote:
>> > > This kind of memcpy() is error-prone. Its replacement with a struct
>> > > assignment is prefered because it's type-safe and much easier to read.
>> > >
>> > > Found by coccinelle. Hand patched and reviewed.
>> > > Tested by compilation only.
>> > >
>> > > A simplified version of the semantic match that finds this problem is as
>> > > follows: (http://coccinelle.lip6.fr/)
>> > >
>> > > // <smpl>
>> > > @@
>> > > identifier struct_name;
>> > > struct struct_name to;
>> > > struct struct_name from;
>> > > expression E;
>> > > @@
>> > > -memcpy(&(to), &(from), E);
>> > > +to = from;
>> > > // </smpl>
>> > >
>> > > Cc: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
>> > > Signed-off-by: Peter Senna Tschudin <peter.senna@gmail.com>
>> > > Signed-off-by: Ezequiel Garcia <elezegarcia@gmail.com>
>> > > ---
>> > >
>> > >  drivers/media/usb/uvc/uvc_v4l2.c |    6 +++---
>> > >  1 files changed, 3 insertions(+), 3 deletions(-)
>> > >
>> > > diff --git a/drivers/media/usb/uvc/uvc_v4l2.c
>> > > b/drivers/media/usb/uvc/uvc_v4l2.c index f00db30..4fc8737 100644
>> > > --- a/drivers/media/usb/uvc/uvc_v4l2.c
>> > > +++ b/drivers/media/usb/uvc/uvc_v4l2.c
>> > > @@ -314,7 +314,7 @@ static int uvc_v4l2_set_format(struct uvc_streaming
>> > > *stream,
>> > >                 goto done;
>> > >         }
>> > >
>> > > -       memcpy(&stream->ctrl, &probe, sizeof probe);
>> > > +       stream->ctrl = probe;
>> > >
>> > >         stream->cur_format = format;
>> > >         stream->cur_frame = frame;
>> > > @@ -386,7 +386,7 @@ static int uvc_v4l2_set_streamparm(struct
>> > > uvc_streaming *stream,
>> > >                 return -EBUSY;
>> > >         }
>> > >
>> > > -       memcpy(&probe, &stream->ctrl, sizeof probe);
>> > > +       probe = stream->ctrl;
>> > >         probe.dwFrameInterval =
>> > >                 uvc_try_frame_interval(stream->cur_frame, interval);
>> > >
>> > > @@ -397,7 +397,7 @@ static int uvc_v4l2_set_streamparm(struct
>> > > uvc_streaming *stream,
>> > >                 return ret;
>> > >         }
>> > >
>> > > -       memcpy(&stream->ctrl, &probe, sizeof probe);
>> > > +       stream->ctrl = probe;
>> > >         mutex_unlock(&stream->mutex);
>> > >
>> > >         /* Return the actual frame period. */
>> >
>> > It seems you've marked this one as "Changes requested" [1].
>> > However, Laurent didn't request any change,
>> > but just pointed out we missed one memcpy replacement candidate.
>> >
>> > I believe it's safe to apply the patch (together with the other 20
>> > patches) and we can fix the missing spot in another patch.
>>
>> The other patches got applied already. Well just do whatever Laurent asked
>> you and re-submit this one ;)
>
> Could you please resubmit this patch with the missed memcpy replaced by a
> struct assignment ? I'll then add it to my tree.
>

Sure!

Thanks,
diff mbox

Patch

diff --git a/drivers/media/usb/uvc/uvc_v4l2.c b/drivers/media/usb/uvc/uvc_v4l2.c
index f00db30..4fc8737 100644
--- a/drivers/media/usb/uvc/uvc_v4l2.c
+++ b/drivers/media/usb/uvc/uvc_v4l2.c
@@ -314,7 +314,7 @@  static int uvc_v4l2_set_format(struct uvc_streaming *stream,
 		goto done;
 	}
 
-	memcpy(&stream->ctrl, &probe, sizeof probe);
+	stream->ctrl = probe;
 	stream->cur_format = format;
 	stream->cur_frame = frame;
 
@@ -386,7 +386,7 @@  static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
 		return -EBUSY;
 	}
 
-	memcpy(&probe, &stream->ctrl, sizeof probe);
+	probe = stream->ctrl;
 	probe.dwFrameInterval =
 		uvc_try_frame_interval(stream->cur_frame, interval);
 
@@ -397,7 +397,7 @@  static int uvc_v4l2_set_streamparm(struct uvc_streaming *stream,
 		return ret;
 	}
 
-	memcpy(&stream->ctrl, &probe, sizeof probe);
+	stream->ctrl = probe;
 	mutex_unlock(&stream->mutex);
 
 	/* Return the actual frame period. */