linux-media.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 03/20] media: usb: uvc_ctrl.c: add temp variable for list iteration
@ 2020-08-07  8:35 Daniel W. S. Almeida
  2020-08-08 20:40 ` Laurent Pinchart
  0 siblings, 1 reply; 4+ messages in thread
From: Daniel W. S. Almeida @ 2020-08-07  8:35 UTC (permalink / raw)
  To: Laurent Pinchart
  Cc: skhan, Daniel W. S. Almeida, Mauro Carvalho Chehab, linux-media,
	linux-kernel

From: "Daniel W. S. Almeida" <dwlsalmeida@gmail.com>

Fixes the following coccinelle report:

drivers/media/usb/uvc/uvc_ctrl.c:1860:5-11:
ERROR: invalid reference to the index variable of the iterator on line 1854

By introducing a temporary variable to iterate the list.

Do not dereference the 'entity' pointer if it is not found in the list.

Found using - Coccinelle (http://coccinelle.lip6.fr)

Signed-off-by: Daniel W. S. Almeida <dwlsalmeida@gmail.com>
---
 drivers/media/usb/uvc/uvc_ctrl.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/drivers/media/usb/uvc/uvc_ctrl.c b/drivers/media/usb/uvc/uvc_ctrl.c
index e399b9fad757..567bdedc2ff2 100644
--- a/drivers/media/usb/uvc/uvc_ctrl.c
+++ b/drivers/media/usb/uvc/uvc_ctrl.c
@@ -1842,7 +1842,8 @@ static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev,
 int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
 	struct uvc_xu_control_query *xqry)
 {
-	struct uvc_entity *entity;
+	struct uvc_entity *entity = NULL;
+	struct uvc_entity *cursor = NULL;
 	struct uvc_control *ctrl;
 	unsigned int i, found = 0;
 	u32 reqflags;
@@ -1851,13 +1852,15 @@ int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
 	int ret;
 
 	/* Find the extension unit. */
-	list_for_each_entry(entity, &chain->entities, chain) {
-		if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
-		    entity->id == xqry->unit)
+	list_for_each_entry(cursor, &chain->entities, chain) {
+		if (UVC_ENTITY_TYPE(cursor) == UVC_VC_EXTENSION_UNIT &&
+		    cursor->id == xqry->unit) {
+			entity = cursor;
 			break;
+		    }
 	}
 
-	if (entity->id != xqry->unit) {
+	if (!entity || entity->id != xqry->unit) {
 		uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
 			xqry->unit);
 		return -ENOENT;
-- 
2.28.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 03/20] media: usb: uvc_ctrl.c: add temp variable for list iteration
  2020-08-07  8:35 [PATCH 03/20] media: usb: uvc_ctrl.c: add temp variable for list iteration Daniel W. S. Almeida
@ 2020-08-08 20:40 ` Laurent Pinchart
  2020-08-08 20:47   ` Laurent Pinchart
  2020-08-10 12:57   ` Daniel W. S. Almeida
  0 siblings, 2 replies; 4+ messages in thread
From: Laurent Pinchart @ 2020-08-08 20:40 UTC (permalink / raw)
  To: Daniel W. S. Almeida
  Cc: skhan, Mauro Carvalho Chehab, linux-media, linux-kernel

Hi Daniel,

Thank you for the patch.

On Fri, Aug 07, 2020 at 05:35:30AM -0300, Daniel W. S. Almeida wrote:
> From: "Daniel W. S. Almeida" <dwlsalmeida@gmail.com>
> 
> Fixes the following coccinelle report:
> 
> drivers/media/usb/uvc/uvc_ctrl.c:1860:5-11:
> ERROR: invalid reference to the index variable of the iterator on line 1854
> 
> By introducing a temporary variable to iterate the list.
> 
> Do not dereference the 'entity' pointer if it is not found in the list.
> 
> Found using - Coccinelle (http://coccinelle.lip6.fr)
> 
> Signed-off-by: Daniel W. S. Almeida <dwlsalmeida@gmail.com>
> ---
>  drivers/media/usb/uvc/uvc_ctrl.c | 13 ++++++++-----
>  1 file changed, 8 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/media/usb/uvc/uvc_ctrl.c b/drivers/media/usb/uvc/uvc_ctrl.c
> index e399b9fad757..567bdedc2ff2 100644
> --- a/drivers/media/usb/uvc/uvc_ctrl.c
> +++ b/drivers/media/usb/uvc/uvc_ctrl.c
> @@ -1842,7 +1842,8 @@ static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev,
>  int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
>  	struct uvc_xu_control_query *xqry)
>  {
> -	struct uvc_entity *entity;
> +	struct uvc_entity *entity = NULL;
> +	struct uvc_entity *cursor = NULL;

cursor doesn't have to be initialized to NULL.

It may be a style preference, but instead of a cursor variable that
doesn't tell in its name what it refers to, I'd prefer a

	bool found = false;

>  	struct uvc_control *ctrl;
>  	unsigned int i, found = 0;
>  	u32 reqflags;
> @@ -1851,13 +1852,15 @@ int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
>  	int ret;
>  
>  	/* Find the extension unit. */
> -	list_for_each_entry(entity, &chain->entities, chain) {
> -		if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
> -		    entity->id == xqry->unit)
> +	list_for_each_entry(cursor, &chain->entities, chain) {
> +		if (UVC_ENTITY_TYPE(cursor) == UVC_VC_EXTENSION_UNIT &&
> +		    cursor->id == xqry->unit) {

All this would keep using entity.

> +			entity = cursor;

And this would be replaced with

			found = true;

>  			break;
> +		    }
>  	}
>  
> -	if (entity->id != xqry->unit) {
> +	if (!entity || entity->id != xqry->unit) {

The second part of the check isn't needed, it was only meant to check if
the entity has been found.

Here, we'd have 

	if (!found) {

I'f you're OK with these changes there's no need to resubmit, I can
update when applying. Please let me know how you'd like to proceed.

>  		uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
>  			xqry->unit);
>  		return -ENOENT;

-- 
Regards,

Laurent Pinchart

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 03/20] media: usb: uvc_ctrl.c: add temp variable for list iteration
  2020-08-08 20:40 ` Laurent Pinchart
@ 2020-08-08 20:47   ` Laurent Pinchart
  2020-08-10 12:57   ` Daniel W. S. Almeida
  1 sibling, 0 replies; 4+ messages in thread
From: Laurent Pinchart @ 2020-08-08 20:47 UTC (permalink / raw)
  To: Daniel W. S. Almeida
  Cc: skhan, Mauro Carvalho Chehab, linux-media, linux-kernel

Hi Daniel,

On Sat, Aug 08, 2020 at 11:40:13PM +0300, Laurent Pinchart wrote:
> On Fri, Aug 07, 2020 at 05:35:30AM -0300, Daniel W. S. Almeida wrote:
> > From: "Daniel W. S. Almeida" <dwlsalmeida@gmail.com>
> > 
> > Fixes the following coccinelle report:
> > 
> > drivers/media/usb/uvc/uvc_ctrl.c:1860:5-11:
> > ERROR: invalid reference to the index variable of the iterator on line 1854
> > 
> > By introducing a temporary variable to iterate the list.
> > 
> > Do not dereference the 'entity' pointer if it is not found in the list.
> > 
> > Found using - Coccinelle (http://coccinelle.lip6.fr)
> > 
> > Signed-off-by: Daniel W. S. Almeida <dwlsalmeida@gmail.com>
> > ---
> >  drivers/media/usb/uvc/uvc_ctrl.c | 13 ++++++++-----
> >  1 file changed, 8 insertions(+), 5 deletions(-)
> > 
> > diff --git a/drivers/media/usb/uvc/uvc_ctrl.c b/drivers/media/usb/uvc/uvc_ctrl.c
> > index e399b9fad757..567bdedc2ff2 100644
> > --- a/drivers/media/usb/uvc/uvc_ctrl.c
> > +++ b/drivers/media/usb/uvc/uvc_ctrl.c
> > @@ -1842,7 +1842,8 @@ static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev,
> >  int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
> >  	struct uvc_xu_control_query *xqry)
> >  {
> > -	struct uvc_entity *entity;
> > +	struct uvc_entity *entity = NULL;
> > +	struct uvc_entity *cursor = NULL;
> 
> cursor doesn't have to be initialized to NULL.
> 
> It may be a style preference, but instead of a cursor variable that
> doesn't tell in its name what it refers to, I'd prefer a
> 
> 	bool found = false;
> 
> >  	struct uvc_control *ctrl;
> >  	unsigned int i, found = 0;
> >  	u32 reqflags;
> > @@ -1851,13 +1852,15 @@ int uvc_xu_ctrl_query(struct uvc_video_chain *chain,
> >  	int ret;
> >  
> >  	/* Find the extension unit. */
> > -	list_for_each_entry(entity, &chain->entities, chain) {
> > -		if (UVC_ENTITY_TYPE(entity) == UVC_VC_EXTENSION_UNIT &&
> > -		    entity->id == xqry->unit)
> > +	list_for_each_entry(cursor, &chain->entities, chain) {
> > +		if (UVC_ENTITY_TYPE(cursor) == UVC_VC_EXTENSION_UNIT &&
> > +		    cursor->id == xqry->unit) {
> 
> All this would keep using entity.
> 
> > +			entity = cursor;
> 
> And this would be replaced with
> 
> 			found = true;
> 
> >  			break;
> > +		    }

I forgot to mention that the indentation is incorrect here.

> >  	}
> >  
> > -	if (entity->id != xqry->unit) {
> > +	if (!entity || entity->id != xqry->unit) {
> 
> The second part of the check isn't needed, it was only meant to check if
> the entity has been found.
> 
> Here, we'd have 
> 
> 	if (!found) {
> 
> I'f you're OK with these changes there's no need to resubmit, I can
> update when applying. Please let me know how you'd like to proceed.
> 
> >  		uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
> >  			xqry->unit);
> >  		return -ENOENT;

-- 
Regards,

Laurent Pinchart

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 03/20] media: usb: uvc_ctrl.c: add temp variable for list iteration
  2020-08-08 20:40 ` Laurent Pinchart
  2020-08-08 20:47   ` Laurent Pinchart
@ 2020-08-10 12:57   ` Daniel W. S. Almeida
  1 sibling, 0 replies; 4+ messages in thread
From: Daniel W. S. Almeida @ 2020-08-10 12:57 UTC (permalink / raw)
  To: Laurent Pinchart; +Cc: skhan, Mauro Carvalho Chehab, linux-media, linux-kernel

Hi Laurent!

> I'f you're OK with these changes there's no need to resubmit, I can
> update when applying. Please let me know how you'd like to proceed.

That's OK with me!


Thanks for reviewing.

- Daniel

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-08-10 12:57 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-07  8:35 [PATCH 03/20] media: usb: uvc_ctrl.c: add temp variable for list iteration Daniel W. S. Almeida
2020-08-08 20:40 ` Laurent Pinchart
2020-08-08 20:47   ` Laurent Pinchart
2020-08-10 12:57   ` Daniel W. S. Almeida

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).