Skip to contentSkip to conversation

media: uvcvideo: fix build error in uvc_ctrl_cleanup_fh

PATCH In review Desnes Nunes 4 participants 7 messages Jun 30, 2025

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

PATCH 1/1 media: uvcvideo: fix build error in uvc_ctrl_cleanup_fh

This fixes the following compilation failure: "error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode"

Fixes 221cd51efe45 ("media: uvcvideo: Remove dangling pointers")
---
 drivers/media/usb/uvc/uvc_ctrl.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/usb/uvc/uvc_ctrl.c +2 -2
index 44b6513c5264..532615d8484b 100644
@@ -3260,7 +3260,7 @@ int uvc_ctrl_init_device(struct uvc_device *dev)
 void uvc_ctrl_cleanup_fh(struct uvc_fh *handle)
 {
 	struct uvc_entity *entity;
-	int i;
+	unsigned int i;
 
 	guard(mutex)(&handle->chain->ctrl_mutex);
 
@@ -3268,7 +3268,7 @@ void uvc_ctrl_cleanup_fh(struct uvc_fh *handle)
 		return;
 
 	list_for_each_entry(entity, &handle->chain->dev->entities, list) {
-		for (unsigned int i = 0; i < entity->ncontrols; ++i) {
+		for (i = 0; i < entity->ncontrols; ++i) {
 			if (entity->controls[i].handle != handle)
 				continue;
 			uvc_ctrl_set_handle(handle, &entity->controls[i], NULL);
Signature · 1 line
2.49.0
#1 lore raw

Hi Desdes

How did you trigger this build warning? I believe we use C11

https://www.kernel.org/doc/html/latest/process/programming-language.html

Regards!

Quoted 37 lines · Desnes Nunes

This fixes the following compilation failure: "error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode"

Fixes 221cd51efe45 ("media: uvcvideo: Remove dangling pointers")
---
 drivers/media/usb/uvc/uvc_ctrl.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/usb/uvc/uvc_ctrl.c +2 -3
index 44b6513c5264..532615d8484b 100644
@@ -3260,7 +3260,7 @@ int uvc_ctrl_init_device(struct uvc_device *dev)
 void uvc_ctrl_cleanup_fh(struct uvc_fh *handle)
 {
        struct uvc_entity *entity;
-       int i;
+       unsigned int i;
 
        guard(mutex)(&handle->chain->ctrl_mutex);
 
@@ -3268,7 +3268,7 @@ void uvc_ctrl_cleanup_fh(struct uvc_fh *handle)
                return;
 
        list_for_each_entry(entity, &handle->chain->dev->entities, list) {
-               for (unsigned int i = 0; i < entity->ncontrols; ++i) {
+               for (i = 0; i < entity->ncontrols; ++i) {
                        if (entity->controls[i].handle != handle)
                                continue;
                        uvc_ctrl_set_handle(handle, &entity->controls[i], NULL);
--

2.49.0

Signature · 1 line
Ricardo Ribalda
maintainer
#2 lore raw
Quoted 5 lines · Ricardo Ribalda

Hi Desdes

How did you trigger this build warning? I believe we use C11

https://www.kernel.org/doc/html/latest/process/programming-language.html

Note that the local declaration of the loop counter shadows the global one. I would have expected a different compiler warning though.

The shadowing was introduced by

commit 10acb9101355484c3e4f2625003cd1b6c203cfe4

Author: Ricardo Ribalda <[email protected]>
Date:   Thu Mar 27 21:05:29 2025 +0000

media: uvcvideo: Increase/decrease the PM counter per IOCTL

and I think it should be fixed (while at it, with an unsigned int local loop counter instead of a signed int) as it's not a good practice.

Quoted 34 lines · Desnes Nunes
Quoted 33 lines

This fixes the following compilation failure: "error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode"

Fixes 221cd51efe45 ("media: uvcvideo: Remove dangling pointers")
---
 drivers/media/usb/uvc/uvc_ctrl.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/usb/uvc/uvc_ctrl.c +2 -2
index 44b6513c5264..532615d8484b 100644
@@ -3260,7 +3260,7 @@ int uvc_ctrl_init_device(struct uvc_device *dev)
 void uvc_ctrl_cleanup_fh(struct uvc_fh *handle)
 {
        struct uvc_entity *entity;
-       int i;
+       unsigned int i;
 
        guard(mutex)(&handle->chain->ctrl_mutex);
 
@@ -3268,7 +3268,7 @@ void uvc_ctrl_cleanup_fh(struct uvc_fh *handle)
                return;
 
        list_for_each_entry(entity, &handle->chain->dev->entities, list) {
-               for (unsigned int i = 0; i < entity->ncontrols; ++i) {
+               for (i = 0; i < entity->ncontrols; ++i) {
                        if (entity->controls[i].handle != handle)
                                continue;
                        uvc_ctrl_set_handle(handle, &entity->controls[i], NULL);
Signature · 3 lines
Regards,

Laurent Pinchart
#3 lore raw

Hello Laurent,

Yes, I did see that commit and will send a v2 to properly address the variable shadowing following C11 standards.

Thanks for the review Laurent,

Quoted 62 lines · Laurent Pinchart
Quoted 5 lines · Ricardo Ribalda

Hi Desdes

How did you trigger this build warning? I believe we use C11

https://www.kernel.org/doc/html/latest/process/programming-language.html

Note that the local declaration of the loop counter shadows the global one. I would have expected a different compiler warning though.

The shadowing was introduced by

commit 10acb9101355484c3e4f2625003cd1b6c203cfe4

Author: Ricardo Ribalda <[email protected]>
Date:   Thu Mar 27 21:05:29 2025 +0000

media: uvcvideo: Increase/decrease the PM counter per IOCTL

and I think it should be fixed (while at it, with an unsigned int local loop counter instead of a signed int) as it's not a good practice.

Quoted 28 lines · Desnes Nunes
Quoted 27 lines

This fixes the following compilation failure: "error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode"

Fixes 221cd51efe45 ("media: uvcvideo: Remove dangling pointers")
---
 drivers/media/usb/uvc/uvc_ctrl.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/usb/uvc/uvc_ctrl.c +1 -1
index 44b6513c5264..532615d8484b 100644
@@ -3260,7 +3260,7 @@ int uvc_ctrl_init_device(struct uvc_device *dev)
 void uvc_ctrl_cleanup_fh(struct uvc_fh *handle)
 {
        struct uvc_entity *entity;
-       int i;
+       unsigned int i;
 
        guard(mutex)(&handle->chain->ctrl_mutex);
 
@@ -3268,7 +3268,7 @@ void uvc_ctrl_cleanup_fh(struct uvc_fh *handle)
                return;
 
Quoted 6 lines

drivers/media/usb/uvc/uvc_ctrl.c void uvc_ctrl_cleanup_fh(struct uvc_fh *handle) · line 3274

Quoted 6 lines
drivers/media/usb/uvc/uvc_ctrl.c +1 -1
        list_for_each_entry(entity, &handle->chain->dev->entities, list) {
-               for (unsigned int i = 0; i < entity->ncontrols; ++i) {
+               for (i = 0; i < entity->ncontrols; ++i) {
                        if (entity->controls[i].handle != handle)
                                continue;
                        uvc_ctrl_set_handle(handle, &entity->controls[i], NULL);

--
Regards,

Laurent Pinchart

Signature · 1 line
Desnes Nunes
#4 lore raw
In reply to Ricardo Ribalda

Hello Ricardo,

I triggered this build error while working in an older codebase that uses C89 due to legacy support reasons.
Indeed - will focus on submitting C11 compatible fixes even when working on older codebases.

Thanks for the review Ricardo,

Quoted 53 lines · Ricardo Ribalda

Hi Desdes

How did you trigger this build warning? I believe we use C11

https://www.kernel.org/doc/html/latest/process/programming-language.html

Regards!

Quoted 34 lines · Desnes Nunes

This fixes the following compilation failure: "error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode"

Fixes 221cd51efe45 ("media: uvcvideo: Remove dangling pointers")
---
 drivers/media/usb/uvc/uvc_ctrl.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/usb/uvc/uvc_ctrl.c +2 -3
index 44b6513c5264..532615d8484b 100644
@@ -3260,7 +3260,7 @@ int uvc_ctrl_init_device(struct uvc_device *dev)
 void uvc_ctrl_cleanup_fh(struct uvc_fh *handle)
 {
        struct uvc_entity *entity;
-       int i;
+       unsigned int i;
 
        guard(mutex)(&handle->chain->ctrl_mutex);
 
@@ -3268,7 +3268,7 @@ void uvc_ctrl_cleanup_fh(struct uvc_fh *handle)
                return;
 
        list_for_each_entry(entity, &handle->chain->dev->entities, list) {
-               for (unsigned int i = 0; i < entity->ncontrols; ++i) {
+               for (i = 0; i < entity->ncontrols; ++i) {
                        if (entity->controls[i].handle != handle)
                                continue;
                        uvc_ctrl_set_handle(handle, &entity->controls[i], NULL);
--
Quoted 3 lines

2.49.0

--
Ricardo Ribalda

Signature · 1 line
Desnes Nunes
maintainer
#5 lore raw
In reply to Desnes Nunes
Quoted 15 lines · Desnes Nunes

This fixes the following compilation failure: "error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode"

Fixes 221cd51efe45 ("media: uvcvideo: Remove dangling pointers")
---
 drivers/media/usb/uvc/uvc_ctrl.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/usb/uvc/uvc_ctrl.c +0 -0
index 44b6513c5264..532615d8484b 100644

drivers/media/usb/uvc/uvc_ctrl.c int uvc_ctrl_init_device(struct uvc_device *dev) · line 3265

Quoted 7 lines
drivers/media/usb/uvc/uvc_ctrl.c +1 -1
@@ -3260,7 +3260,7 @@ int uvc_ctrl_init_device(struct uvc_device *dev)
 void uvc_ctrl_cleanup_fh(struct uvc_fh *handle)
 {
 	struct uvc_entity *entity;
-	int i;
+	unsigned int i;
 
 	guard(mutex)(&handle->chain->ctrl_mutex);

If your compiler can handle this guard(mutex) line, then:

drivers/media/usb/uvc/uvc_ctrl.c void uvc_ctrl_cleanup_fh(struct uvc_fh *handle) · line 3271

Quoted 6 lines
drivers/media/usb/uvc/uvc_ctrl.c +0 -1
@@ -3268,7 +3268,7 @@ void uvc_ctrl_cleanup_fh(struct uvc_fh *handle)
 		return;
 
 	list_for_each_entry(entity, &handle->chain->dev->entities, list) {
-		for (unsigned int i = 0; i < entity->ncontrols; ++i) {

It can also handle that line.

Are you sure you are using a sane compiler?

confused,

greg k-h

#6 lore raw

Hello Greg,

The compiler is sane, but I ran into this while backporting fixes to an old codebase that still uses C89 due to legacy support reasons. Furthermore, you're right; there is no guard() in my code base, thus I had to backport guard() with the old mutex lock/unlock calls used prior to guard().
Indeed - will focus on all of what has been said on the v2.

Thanks for the review Greg,

Quoted 42 lines · Greg KH
Quoted 15 lines · Desnes Nunes

This fixes the following compilation failure: "error: ‘for’ loop initial declarations are only allowed in C99 or C11 mode"

Fixes 221cd51efe45 ("media: uvcvideo: Remove dangling pointers")
---
 drivers/media/usb/uvc/uvc_ctrl.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/media/usb/uvc/uvc_ctrl.c +0 -0
index 44b6513c5264..532615d8484b 100644

drivers/media/usb/uvc/uvc_ctrl.c int uvc_ctrl_init_device(struct uvc_device *dev) · line 3265

Quoted 7 lines
drivers/media/usb/uvc/uvc_ctrl.c +1 -1
@@ -3260,7 +3260,7 @@ int uvc_ctrl_init_device(struct uvc_device *dev)
 void uvc_ctrl_cleanup_fh(struct uvc_fh *handle)
 {
      struct uvc_entity *entity;
-     int i;
+     unsigned int i;
 
      guard(mutex)(&handle->chain->ctrl_mutex);

If your compiler can handle this guard(mutex) line, then:

drivers/media/usb/uvc/uvc_ctrl.c void uvc_ctrl_cleanup_fh(struct uvc_fh *handle) · line 3271

Quoted 6 lines
drivers/media/usb/uvc/uvc_ctrl.c +0 -1
@@ -3268,7 +3268,7 @@ void uvc_ctrl_cleanup_fh(struct uvc_fh *handle)
              return;
 
      list_for_each_entry(entity, &handle->chain->dev->entities, list) {
-             for (unsigned int i = 0; i < entity->ncontrols; ++i) {

It can also handle that line.

Are you sure you are using a sane compiler?

confused,

greg k-h

Signature · 1 line
Desnes Nunes