mfd: arizona: fix undefined behavior
Subsystems (2)
PATCH 1/1 mfd: arizona: fix undefined behavior
When the driver is used with a subdevice that is disabled in the kernel configuration, clang gets a little confused about the control flow and fails to notice that n_subdevs is only uninitialized when subdevs is NULL, and we check for that, leading to a false-positive warning:
drivers/mfd/arizona-core.c:1423:19: error: variable 'n_subdevs' is uninitialized when used here
[-Werror,-Wuninitialized]
subdevs, n_subdevs, NULL, 0, NULL);
^~~~~~~~~
drivers/mfd/arizona-core.c:999:15: note: initialize the variable 'n_subdevs' to silence this warning
int n_subdevs, ret, i;
^
= 0Ideally, we would rearrange the code to avoid all those early initializations and have an explicit exit in each disabled case, but it's much easier to chicken out and add one more initialization here to shut up the warning.
--- drivers/mfd/arizona-core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/mfd/arizona-core.c +1 -1
@@ -996,7 +996,7 @@ int arizona_dev_init(struct arizona *arizona)unsigned int reg, val;int (*apply_patch)(struct arizona *) =NULL ;const struct mfd_cell *subdevs =NULL ; -int n_subdevs, ret, i; +int n_subdevs =0 , ret, i;dev_set_drvdata (arizona->dev, arizona);mutex_init (&arizona->clk_lock);
Signature · 1 line
2.20.0
Quoted 15 lines · Arnd Bergmann
When the driver is used with a subdevice that is disabled in the kernel configuration, clang gets a little confused about the control flow and fails to notice that
n_subdevsis only uninitialized when subdevs is NULL, and we check for that, leading to a false-positive warning:
drivers/mfd/arizona-core.c:1423:19: error: variable 'n_subdevs' is uninitialized when used here[-Werror,-Wuninitialized] subdevs, n_subdevs, NULL, 0, NULL); ^~~~~~~~~ drivers/mfd/arizona-core.c:999:15: note: initialize the variable 'n_subdevs' to silence this warning int n_subdevs, ret, i; ^ = 0
Quoted 6 lines
Ideally, we would rearrange the code to avoid all those early initializations and have an explicit exit in each disabled case, but it's much easier to chicken out and add one more initialization here to shut up the warning.
Signed-off-by Arnd Bergmann <[email protected]>
Took me a bit to follow the flow of this function. I agree that without restructuring it, zero initializing this variable to shut up the warning is the path of least resistance (clang must evaluate variables in isolation like I did until I fully read the commit message *facepalm*).
Quoted 20 lines
--- drivers/mfd/arizona-core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/mfd/arizona-core.c +1
-1@@ -996,7 +996,7 @@ int arizona_dev_init(struct arizona *arizona)unsigned int reg, val;int (*apply_patch)(struct arizona *) =NULL ;const struct mfd_cell *subdevs =NULL ; -int n_subdevs, ret, i; +int n_subdevs =0 , ret, i;dev_set_drvdata (arizona->dev, arizona);mutex_init (&arizona->clk_lock);Signature · 1 line
2.20.0
Quoted 15 lines · Arnd Bergmann
When the driver is used with a subdevice that is disabled in the kernel configuration, clang gets a little confused about the control flow and fails to notice that
n_subdevsis only uninitialized when subdevs is NULL, and we check for that, leading to a false-positive warning:
drivers/mfd/arizona-core.c:1423:19: error: variable 'n_subdevs' is uninitialized when used here[-Werror,-Wuninitialized] subdevs, n_subdevs, NULL, 0, NULL); ^~~~~~~~~ drivers/mfd/arizona-core.c:999:15: note: initialize the variable 'n_subdevs' to silence this warning int n_subdevs, ret, i; ^ = 0
Quoted 7 lines
Ideally, we would rearrange the code to avoid all those early initializations and have an explicit exit in each disabled case, but it's much easier to chicken out and add one more initialization here to shut up the warning.
Signed-off-by Arnd Bergmann <[email protected]>---
Thanks,
Charles