regulator: core: fix error path for regulator_set_voltage_unlocked
PATCH 1/1 regulator: core: fix error path for regulator_set_voltage_unlocked
During several error paths in the function regulator_set_voltage_unlocked() the value of 'ret' can take on negative error values. However, in calls that go through the 'goto out' statement, this return value is lost and return 0 is used instead, indicating a 'pass'.
There are several cases where this function should legitimately return a fail instead of a pass: one such case includes constraints check during voltage selection in the call to regulator_check_voltage(), which can have -EINVAL for the case when an unsupported voltage is incorrectly requested. In that case, -22 is expected as the return value, not 0.
--- drivers/regulator/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/regulator/core.c +1 -1
@@ -3326,7 +3326,7 @@ static int regulator_set_voltage_unlocked(struct regulator *regulator, goto out2; out: -return 0 ; +return ret; out2: voltage->min_uV = old_min_uV; voltage->max_uV = old_max_uV;
Signature · 1 line
1.9.3
18.03.2019 18:32, Steve Twiss пишет:
Quoted 24 lines
During several error paths in the function
regulator_set_voltage_unlocked()the value of 'ret' can take on negative error values. However, in calls that go through the 'goto out' statement, this return value is lost and return 0 is used instead, indicating a 'pass'.There are several cases where this function should legitimately return a fail instead of a pass: one such case includes constraints check during voltage selection in the call to
regulator_check_voltage(), which can have-EINVALfor the case when an unsupported voltage is incorrectly requested. In that case, -22 is expected as the return value, not 0.Fixes 9243a195be7a ("regulator: core: Change voltage setting path")Signed-off-by Steve Twiss <[email protected]>--- drivers/regulator/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/regulator/core.c +0
-0
drivers/regulator/core.c static int regulator_set_voltage_unlocked(struct regulator *regulator, · line 3332
Quoted 8 lines
drivers/regulator/core.c +1
-1@@ -3326,7 +3326,7 @@ static int regulator_set_voltage_unlocked(struct regulator *regulator, goto out2; out: -return 0 ; +return ret; out2: voltage->min_uV = old_min_uV; voltage->max_uV = old_max_uV;
Looks like a good catch.
Probably will be a bit better to write this as:
/* for not coupled regulators this will just set the voltage */
ret = regulator_balance_voltage(rdev, state);- if (ret < 0)
- goto out2;
-
+ if (ret < 0) {
+ voltage->min_uV = old_min_uV;
+ voltage->max_uV = old_max_uV;
+ }
out:
- return 0;
-out2:
- voltage->min_uV = old_min_uV;
- voltage->max_uV = old_max_uV;
-
return ret;
}
18.03.2019 19:03, Dmitry Osipenko пишет:
Quoted 46 lines
18.03.2019 18:32, Steve Twiss пишет:
Quoted 24 lines
During several error paths in the function
regulator_set_voltage_unlocked()the value of 'ret' can take on negative error values. However, in calls that go through the 'goto out' statement, this return value is lost and return 0 is used instead, indicating a 'pass'.There are several cases where this function should legitimately return a fail instead of a pass: one such case includes constraints check during voltage selection in the call to
regulator_check_voltage(), which can have-EINVALfor the case when an unsupported voltage is incorrectly requested. In that case, -22 is expected as the return value, not 0.Fixes 9243a195be7a ("regulator: core: Change voltage setting path")Signed-off-by Steve Twiss <[email protected]>--- drivers/regulator/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/regulator/core.c +0
-0drivers/regulator/core.c static int regulator_set_voltage_unlocked(struct regulator *regulator, · line 3332
Quoted 8 lines
drivers/regulator/core.c +1
-1@@ -3326,7 +3326,7 @@ static int regulator_set_voltage_unlocked(struct regulator *regulator, goto out2; out: -return 0 ; +return ret; out2: voltage->min_uV = old_min_uV; voltage->max_uV = old_max_uV;Looks like a good catch.
Probably will be a bit better to write this as:
/* for not coupled regulators this will just set the voltage */ ret = regulator_balance_voltage(rdev, state);(quoted diff) +2
-3- if (ret < 0) - goto out2; - + if (ret < 0) { + voltage->min_uV = old_min_uV;
Review
Quoted 10 lines
(quoted diff) +2
-5+ voltage->max_uV = old_max_uV; + } out: - return 0; -out2: - voltage->min_uV = old_min_uV; - voltage->max_uV = old_max_uV; - return ret; }
Also, probably won't hurt to add a stable tag "Cc: stable <[email protected]>" to get the fix backported.
Hi Dmitry,
Thanks,
Quoted 49 lines · Dmitry Osipenko
Subject: Re: [PATCH] regulator: core: fix error path for
regulator_set_voltage_unlocked18.03.2019 18:32, Steve Twiss пишет:
drivers/regulator/core.c static int regulator_set_voltage_unlocked(struct
Quoted 23 lines
During several error paths in the function
regulator_set_voltage_unlocked()the value of 'ret' can take on negative error values. However, in calls that go through the 'goto out' statement, this return value is lost and return 0 is used instead, indicating a 'pass'.There are several cases where this function should legitimately return a fail instead of a pass: one such case includes constraints check during voltage selection in the call to
regulator_check_voltage(), which can have-EINVALfor the case when an unsupported voltage is incorrectly requested. In that case, -22 is expected as the return value, not 0.Fixes 9243a195be7a ("regulator: core: Change voltage setting path")Signed-off-by Steve Twiss <[email protected]>--- drivers/regulator/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/regulator/core.c +0
-0@@ -3326,7 +3326,7 @@ static int regulator_set_voltage_unlocked(structregulator *regulator,
drivers/regulator/core.c static int regulator_set_voltage_unlocked(struct regulator *regulator, · line 3332
Quoted 9 lines
drivers/regulator/core.c +1
-1goto out2; out: -return 0 ; +return ret; out2: voltage->min_uV = old_min_uV; voltage->max_uV = old_max_uV;Looks like a good catch.
Probably will be a bit better to write this as:
/* for not coupled regulators this will just set the voltage */ ret = regulator_balance_voltage(rdev, state);(quoted diff) +2
-3- if (ret < 0) - goto out2; - + if (ret < 0) { + voltage->min_uV = old_min_uV;
Review
Quoted 10 lines
(quoted diff) +2
-5+ voltage->max_uV = old_max_uV; + } out: - return 0; -out2: - voltage->min_uV = old_min_uV; - voltage->max_uV = old_max_uV; - return ret; }
I've just had a very similar conversation with Adam Thomson who sits near me and also said the two gotos make it look confusing.
Honestly -- I wasn't convinced because it looked obvious to me, but you are the second person to say it ..
CC: Adam Thomson
So, ok. Agreed. :)
I'll make the change and resend.
Regards,
Steve