Skip to contentSkip to conversation

media: usb: uvc_ctrl.c: add temp variable for list iteration

PATCH In review Daniel W. S. Almeida 2 participants 4 messages Aug 7, 2020 – Aug 10, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 3/20 media: usb: uvc_ctrl.c: add temp variable for list iteration

From: "Daniel W. S. Almeida" <[email protected]>

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)

---
 drivers/media/usb/uvc/uvc_ctrl.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/media/usb/uvc/uvc_ctrl.c +8 -5
index e399b9fad757..567bdedc2ff2 100644
@@ -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;
Signature · 1 line
2.28.0
maintainer
#1 lore raw

Hi Daniel,

Thank you for the patch.

Quoted 23 lines · Daniel W. S. Almeida

From: "Daniel W. S. Almeida" <[email protected]>

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)

---
 drivers/media/usb/uvc/uvc_ctrl.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/media/usb/uvc/uvc_ctrl.c +0 -0
index e399b9fad757..567bdedc2ff2 100644

drivers/media/usb/uvc/uvc_ctrl.c static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev, · line 1846

Quoted 6 lines
drivers/media/usb/uvc/uvc_ctrl.c +2 -1
@@ -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;

drivers/media/usb/uvc/uvc_ctrl.c int uvc_xu_ctrl_query(struct uvc_video_chain *chain, · line 1857

Quoted 13 lines
drivers/media/usb/uvc/uvc_ctrl.c +3 -3
 	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.

drivers/media/usb/uvc/uvc_ctrl.c int uvc_xu_ctrl_query(struct uvc_video_chain *chain, · line 1858

Quoted 1 line
drivers/media/usb/uvc/uvc_ctrl.c +1 -0
+			entity = cursor;

And this would be replaced with

			found = true;

drivers/media/usb/uvc/uvc_ctrl.c int uvc_xu_ctrl_query(struct uvc_video_chain *chain, · line 1863

Quoted 6 lines
drivers/media/usb/uvc/uvc_ctrl.c +2 -1
 			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.

Quoted 3 lines
drivers/media/usb/uvc/uvc_ctrl.c +0 -0
 		uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
 			xqry->unit);
 		return -ENOENT;
Signature · 3 lines
Regards,

Laurent Pinchart
maintainer
#2 lore raw

Hi Daniel,

Quoted 54 lines · Laurent Pinchart
Quoted 23 lines · Daniel W. S. Almeida

From: "Daniel W. S. Almeida" <[email protected]>

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)

---
 drivers/media/usb/uvc/uvc_ctrl.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
drivers/media/usb/uvc/uvc_ctrl.c +0 -0
index e399b9fad757..567bdedc2ff2 100644

drivers/media/usb/uvc/uvc_ctrl.c static int uvc_ctrl_init_xu_ctrl(struct uvc_device *dev, · line 1846

Quoted 6 lines
drivers/media/usb/uvc/uvc_ctrl.c +2 -1
@@ -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;

drivers/media/usb/uvc/uvc_ctrl.c int uvc_xu_ctrl_query(struct uvc_video_chain *chain, · line 1857

Quoted 13 lines
drivers/media/usb/uvc/uvc_ctrl.c +3 -3
 	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.

Quoted 8 lines

drivers/media/usb/uvc/uvc_ctrl.c int uvc_xu_ctrl_query(struct uvc_video_chain *chain, · line 1858

Quoted 1 line
drivers/media/usb/uvc/uvc_ctrl.c +1 -0
+			entity = cursor;

And this would be replaced with

			found = true;

drivers/media/usb/uvc/uvc_ctrl.c int uvc_xu_ctrl_query(struct uvc_video_chain *chain, · line 1860

Quoted 2 lines
drivers/media/usb/uvc/uvc_ctrl.c +1 -0
 			break;
+		    }

I forgot to mention that the indentation is incorrect here.

Quoted 18 lines

drivers/media/usb/uvc/uvc_ctrl.c int uvc_xu_ctrl_query(struct uvc_video_chain *chain, · line 1863

Quoted 4 lines
drivers/media/usb/uvc/uvc_ctrl.c +1 -1
 	}
 
-	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.

Quoted 3 lines
drivers/media/usb/uvc/uvc_ctrl.c +0 -0
 		uvc_trace(UVC_TRACE_CONTROL, "Extension unit %u not found.\n",
 			xqry->unit);
 		return -ENOENT;
Signature · 3 lines
Regards,

Laurent Pinchart
#3 lore raw
In reply to Laurent Pinchart

Hi Laurent!

Quoted 2 lines

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