regulator: max8925: fix compiler warnings
PATCH 1/1 regulator: max8925: fix compiler warnings
From: Qing Xu <[email protected]>
Fixed following compiler warning:
- drivers/regulator/max8925-regulator.c:269:51: warning:
'regulator_idx' may be used uninitialized in this function [-Wmaybe-uninitialized]
--- drivers/regulator/max8925-regulator.c | 3 ++- 1 files changed, 2 insertions(+), 1 deletions(-)
drivers/regulator/max8925-regulator.c +2 -1
@@ -282,7 +282,8 @@ static int __devinit max8925_regulator_probe(struct platform_device *pdev)struct max8925_regulator_info *ri;struct resource *res;struct regulator_dev *rdev; -int i, regulator_idx; +int i; +int regulator_idx =0 ; res =platform_get_resource (pdev,IORESOURCE_REG ,0 );if (!res) {
Signature · 1 line
1.7.0.4
drivers/regulator/max8925-regulator.c static int __devinit max8925_regulator_probe(struct platform_device *pdev) · line 286
Quoted 3 lines · Qing Xu
drivers/regulator/max8925-regulator.c +2
-1-int i, regulator_idx; +int i; +int regulator_idx =0 ;
This sort of fix is rarely good without some analysis as to why this is a sensible initialisation to do, just unconditionally initialising may be masking a real issue in the control flow which the compiler has identified.
Quoted 9 lines · Mark Brown
drivers/regulator/max8925-regulator.c static int __devinit max8925_regulator_probe(struct platform_device *pdev) · line 286
Quoted 3 lines · Qing Xu
drivers/regulator/max8925-regulator.c +2
-1-int i, regulator_idx; +int i; +int regulator_idx =0 ;This sort of fix is rarely good without some analysis as to why this is a sensible initialisation to do, just unconditionally initialising may be masking a real issue in the control flow which the compiler has identified.
In my build environment, there is no such compiler warning. :(
Adding this patch is just want to avoid kbuild test robot's warning. But, in fact, it is not necessary to initialize regulator_idx.
for (i = 0; i < ARRAY_SIZE(max8925_regulator_info); i++) {
ri = &max8925_regulator_info[i];
if (ri->vol_reg == res->start) {****** if regulator_idx can not get a match "i" here, it will return -EINVAL in below code
regulator_idx = i;
break;
}
}
if (i == ARRAY_SIZE(max8925_regulator_info)) {
dev_err(&pdev->dev, "Failed to find regulator %llu\n",
(unsigned long long)res->start);
return -EINVAL;
}How to solve such compiler warning?
Quoted 20 lines · Qing Xu
But, in fact, it is not necessary to initialize
regulator_idx.for (i = 0; i < ARRAY_SIZE(max8925_regulator_info); i++) { ri = &max8925_regulator_info[i]; if (ri->vol_reg == res->start) {****** if
regulator_idxcan not get a match "i" here, it will return-EINVALin below coderegulator_idx = i; break; } } if (i == ARRAY_SIZE(max8925_regulator_info)) { dev_err(&pdev->dev, "Failed to find regulator %llu\n", (unsigned long long)res->start); return -EINVAL; }
Quoted 1 line
How to solve such compiler warning?
Typically by reporting a compiler bug, though sometimes in the process of doing that one finds out that there's some non-obvious way in which the code can break.
Quoted 18 lines · Mark Brown
Quoted 15 lines · Qing Xu
But, in fact, it is not necessary to initialize
regulator_idx.for (i = 0; i < ARRAY_SIZE(max8925_regulator_info); i++) { ri = &max8925_regulator_info[i]; if (ri->vol_reg == res->start) { ****** if regulator_idx can not get a match "i" here, it will return -EINVAL in below code regulator_idx = i; break; } } if (i == ARRAY_SIZE(max8925_regulator_info)) { dev_err(&pdev->dev, "Failed to find regulator %llu\n", (unsigned long long)res->start); return -EINVAL; }Quoted 1 line
How to solve such compiler warning?
Quoted 3 lines
Typically by reporting a compiler bug, though sometimes in the process of doing that one finds out that there's some non-obvious way in which the code can break.
It seems not like a compiler bug, its logic is:
for(...; i<xxx; ...) {
if (...) {
regulator_idx = i
break;
}
}if (i == xxx)
return ERROR;
If regulator_idx can not get a matched "i" value, code will return ERROR. But it seems that compiler can not do so complex judge.
And, I think the code is safe even if regulator_idx is not initialized, also because of the "return ERROR" judge.