pci: Fix device_find_first_child() return value handling
PATCH 1/1 pci: Fix device_find_first_child() return value handling
This function only ever returns 0, but may not assign the second parameter. Same thing for device_find_next_child(). Do not assign ret to stop proliferation of this misuse.
---
Cc: "Pali Rohár" <[email protected]>
Cc: Bin Meng <[email protected]>
Cc: Marek Vasut <[email protected]>
Cc: Michal Suchanek <[email protected]>
Cc: Simon Glass <[email protected]>
--- drivers/pci/pci-uclass.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/pci/pci-uclass.c +3 -3
@@ -545,9 +545,9 @@ int pci_auto_config_devices(struct udevice *bus) sub_bus =dev_seq (bus);debug ("%s: start\n" , __func__);pciauto_config_init (hose); -for (ret =device_find_first_child (bus, &dev); - !ret && dev; - ret =device_find_next_child (&dev)) { +for (device_find_first_child (bus, &dev); + dev; +device_find_next_child (&dev)) {unsigned int max_bus;int ret;
Signature · 1 line
2.40.1
Hello,
Quoted 21 lines · Marek Vasut
This function only ever returns 0, but may not assign the second parameter. Same thing for
device_find_next_child(). Do not assign ret to stop proliferation of this misuse.Reported-by Jonas Karlman <[email protected]>Signed-off-by Marek Vasut <[email protected]>---
Cc: "Pali Rohár" <[email protected]>
Cc: Bin Meng <[email protected]>
Cc: Marek Vasut <[email protected]>
Cc: Michal Suchanek <[email protected]>
Cc: Simon Glass <[email protected]>--- drivers/pci/pci-uclass.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)drivers/pci/pci-uclass.c +0
-0
drivers/pci/pci-uclass.c int pci_auto_config_devices(struct udevice *bus) · line 550
Quoted 9 lines
drivers/pci/pci-uclass.c +3
-3@@ -545,9 +545,9 @@ int pci_auto_config_devices(struct udevice *bus) sub_bus =dev_seq (bus);debug ("%s: start\n" , __func__);pciauto_config_init (hose); -for (ret =device_find_first_child (bus, &dev); - !ret && dev; - ret =device_find_next_child (&dev)) { +for (device_find_first_child (bus, &dev); + dev; +device_find_next_child (&dev)) {
Sounds like you will need to remove the declaration of the now unused ret variable as well.
More generally, what is the overall vision for these functions returning always zero?
Should the return value be kept in case the underlying implementation changes and errors can happen in the future, and consequently checked?
Should the return value be removed when meaningless making these useless assignments and checks an error?
I already elimimnated a return value where using it lead to incorrect behavior but here using it or not is equally correct with the current implementation.
Thanks
Michal
Quoted 43 lines · Michal Suchánek
Hello,
Quoted 21 lines · Marek Vasut
This function only ever returns 0, but may not assign the second parameter. Same thing for
device_find_next_child(). Do not assign ret to stop proliferation of this misuse.Reported-by Jonas Karlman <[email protected]>Signed-off-by Marek Vasut <[email protected]>---
Cc: "Pali Rohár" <[email protected]>
Cc: Bin Meng <[email protected]>
Cc: Marek Vasut <[email protected]>
Cc: Michal Suchanek <[email protected]>
Cc: Simon Glass <[email protected]>--- drivers/pci/pci-uclass.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)drivers/pci/pci-uclass.c +0
-0drivers/pci/pci-uclass.c int pci_auto_config_devices(struct udevice *bus) · line 550
Quoted 9 lines
drivers/pci/pci-uclass.c +3
-3@@ -545,9 +545,9 @@ int pci_auto_config_devices(struct udevice *bus) sub_bus =dev_seq (bus);debug ("%s: start\n" , __func__);pciauto_config_init (hose); -for (ret =device_find_first_child (bus, &dev); - !ret && dev; - ret =device_find_next_child (&dev)) { +for (device_find_first_child (bus, &dev); + dev; +device_find_next_child (&dev)) {Sounds like you will need to remove the declaration of the now unused ret variable as well.
More generally, what is the overall vision for these functions returning always zero?
Should the return value be kept in case the underlying implementation changes and errors can happen in the future, and consequently checked?
Quoted 6 lines
Should the return value be removed when meaningless making these useless assignments and checks an error?
I already elimimnated a return value where using it lead to incorrect behavior but here using it or not is equally correct with the current implementation.
Probably a question for Simon, really. Personally I would be tempted to switch the function to return void.
Simon Glass
Reviewed-byHi Marek,
Quoted 21 lines · Marek Vasut
Quoted 19 lines · Michal Suchánek
Hello,
Quoted 16 lines · Marek Vasut
This function only ever returns 0, but may not assign the second parameter. Same thing for
device_find_next_child(). Do not assign ret to stop proliferation of this misuse.Reported-by Jonas Karlman <[email protected]>Signed-off-by Marek Vasut <[email protected]>---
Cc: "Pali Rohár" <[email protected]>
Cc: Bin Meng <[email protected]>
Cc: Marek Vasut <[email protected]>
Cc: Michal Suchanek <[email protected]>
Cc: Simon Glass <[email protected]>--- drivers/pci/pci-uclass.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
drivers/pci/pci-uclass.c +0
-0@@ -545,9 +545,9 @@ int pci_auto_config_devices(struct udevice *bus) sub_bus =dev_seq (bus);Quoted 8 lines
drivers/pci/pci-uclass.c int pci_auto_config_devices(struct udevice *bus) · line 550
Quoted 8 lines
drivers/pci/pci-uclass.c +3
-3debug ("%s: start\n" , __func__);pciauto_config_init (hose); -for (ret =device_find_first_child (bus, &dev); - !ret && dev; - ret =device_find_next_child (&dev)) { +for (device_find_first_child (bus, &dev); + dev; +device_find_next_child (&dev)) {
Quoted 3 lines
Quoted 3 lines
Sounds like you will need to remove the declaration of the now unused ret variable as well.
Yes, please remove the 'ret' at the top of the function.
Quoted 10 lines
Quoted 10 lines
More generally, what is the overall vision for these functions returning always zero?
Should the return value be kept in case the underlying implementation changes and errors can happen in the future, and consequently checked?
Should the return value be removed when meaningless making these useless assignments and checks an error?
Quoted 6 lines
Quoted 3 lines
I already elimimnated a return value where using it lead to incorrect behavior but here using it or not is equally correct with the current implementation.
Probably a question for Simon, really. Personally I would be tempted to switch the function to return void.
So long as the function has its meaning documented, I think it is OK. As a separate patch, I am OK with changing device_find_first/next_child() to void, or alternatively having them return 0 on success and -ENODEV if nothing was found.
Regards,
Simon
Hello,
Quoted 5 lines · Simon Glass
Hi Marek,
Quoted 2 lines · Marek Vasut
On 7/17/23 09:42, Michal Suchánek wrote:
...
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
More generally, what is the overall vision for these functions returning always zero?
Should the return value be kept in case the underlying implementation changes and errors can happen in the future, and consequently checked?
Should the return value be removed when meaningless making these useless assignments and checks an error?
I already elimimnated a return value where using it lead to incorrect behavior but here using it or not is equally correct with the current implementation.
Quoted 7 lines
Quoted 2 lines
Probably a question for Simon, really. Personally I would be tempted to switch the function to return void.
So long as the function has its meaning documented, I think it is OK. As a separate patch, I am OK with changing device_find_first/next_child() to void, or alternatively having them return 0 on success and
-ENODEVif nothing was found.
I think when there is one error condition having two ways to report it is one too many.
Thanks
Michal
Quoted 7 lines · Marek Vasut
This function only ever returns 0, but may not assign the second parameter. Same thing for
device_find_next_child(). Do not assign ret to stop proliferation of this misuse.Reported-by Jonas Karlman <[email protected]>Signed-off-by Marek Vasut <[email protected]>Reviewed-by Simon Glass <[email protected]>
Applied to u-boot/next, thanks!
Signature · 1 line
Tom