Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drivers: led: Fix uninitialized variable #82067

Closed
wants to merge 1 commit into from

Conversation

rruuaanng
Copy link
Collaborator

Fix uninitialized ret variables scanned by Coverity.

from:
#81958

Fix uninitialized ret variables scanned by Coverity.

Signed-off-by: James Roy <[email protected]>
@zephyrbot zephyrbot added size: XS A PR changing only a single line of code area: LED Label to identify LED subsystem labels Nov 26, 2024
@@ -80,7 +80,7 @@ static int is31fl3194_set_color(const struct device *dev, uint32_t led, uint8_t
{
const struct is31fl3194_config *config = dev->config;
const struct led_info *info = is31fl3194_led_to_info(config, led);
int ret;
int ret = 0;
Copy link
Collaborator

@simonguinot simonguinot Nov 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, first there is actually no case where ret is tested/used uninitialized with the current code. This can only happen if the color_mapping is invalid. But it is tested by is31fl3194_check_config().

That being said, it is interesting to consolidate the code...

About this patch, it is not optimal to initialize ret with 0. This opens the door to update the LED even if the channels have not been updated.

Since there is no point in continuing at sending I2C I/Os if one of them fails, then I would prefer something like that:

                ret = i2c_reg_write_byte_dt(&config->bus, led_channels[i], value);
                if (ret != 0) 
                        goto err;
        }

        ret = i2c_reg_write_byte_dt(&config->bus,
                                    IS31FL3194_UPDATE_REG,
                                    IS31FL3194_UPDATE_VAL);

err:
        if (ret != 0) {
                LOG_ERR("%s: LED write failed: %d", dev->name, ret);
        }

        return ret;
}

Copy link
Collaborator Author

@rruuaanng rruuaanng Nov 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to have kept you waiting, I think your suggestion is very good. After ret is initialized to 0, we should remove the if, I will fix it in two commits :)

Edit

Hold on, we seem to have forgotten the i2c_reg_write_byte_dt above. Its main function is to execute i2c_reg_write_byte_dt in the if below after all the i2c_reg_write_byte_dt above succeed. It seems that it makes sense to initialize ret to 0.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I don't see what is wrong this the code example I proposed. The last call to i2c_reg_write_byte_dt is only executed if the color registers are updated.

@simonguinot
Copy link
Collaborator

Hi @rruuaanng. Thanks for taking care of this issue. Please see my comment above.

@jilaypandya
Copy link
Contributor

Hi @rruuaanng, is it okay if we close this PR. Since #82679 is already merged, let's see if sca is happy with the solution or not :)

@rruuaanng rruuaanng closed this Dec 19, 2024
@rruuaanng rruuaanng deleted the uninit-var-led branch December 19, 2024 23:49
@rruuaanng
Copy link
Collaborator Author

Thank you for your reminder. I have closed it.

@rruuaanng
Copy link
Collaborator Author

I agree with @simonguinot suggestion, but it is likely to be regarded as an appearance change. I think we can change it in the future. That is, when there are other problems with the driver. So, thank you :--)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: LED Label to identify LED subsystem size: XS A PR changing only a single line of code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants