Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

regulator: s5m8767: fix get_register() error handling

The s5m8767_pmic_probe() function calls s5m8767_get_register() to
read data without checking the return code, which produces a compile-time
warning when that data is accessed:

drivers/regulator/s5m8767.c: In function 's5m8767_pmic_probe':
drivers/regulator/s5m8767.c:924:7: error: 'enable_reg' may be used uninitialized in this function [-Werror=maybe-uninitialized]
drivers/regulator/s5m8767.c:944:30: error: 'enable_val' may be used uninitialized in this function [-Werror=maybe-uninitialized]

This changes the s5m8767_get_register() function to return a -EINVAL
not just for an invalid register number but also for an invalid
regulator number, as both would result in returning uninitialized
data. The s5m8767_pmic_probe() function is then changed accordingly
to fail on a read error, as all the other callers of s5m8767_get_register()
already do.

In practice this probably cannot happen, as we don't call
s5m8767_get_register() with invalid arguments, but the gcc
warning seems valid in principle, in terms writing safe
error checking.

Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Fixes: 9c4c60554acf ("regulator: s5m8767: Convert to use regulator_[enable|disable|is_enabled]_regmap")
Signed-off-by: Mark Brown <broonie@kernel.org>

authored by

Arnd Bergmann and committed by
Mark Brown
e07ff943 92e963f5

+9 -4
+9 -4
drivers/regulator/s5m8767.c
··· 202 202 } 203 203 } 204 204 205 - if (i < s5m8767->num_regulators) 206 - *enable_ctrl = 207 - s5m8767_opmode_reg[reg_id][mode] << S5M8767_ENCTRL_SHIFT; 205 + if (i >= s5m8767->num_regulators) 206 + return -EINVAL; 207 + 208 + *enable_ctrl = s5m8767_opmode_reg[reg_id][mode] << S5M8767_ENCTRL_SHIFT; 208 209 209 210 return 0; 210 211 } ··· 938 937 else 939 938 regulators[id].vsel_mask = 0xff; 940 939 941 - s5m8767_get_register(s5m8767, id, &enable_reg, 940 + ret = s5m8767_get_register(s5m8767, id, &enable_reg, 942 941 &enable_val); 942 + if (ret) { 943 + dev_err(s5m8767->dev, "error reading registers\n"); 944 + return ret; 945 + } 943 946 regulators[id].enable_reg = enable_reg; 944 947 regulators[id].enable_mask = S5M8767_ENCTRL_MASK; 945 948 regulators[id].enable_val = enable_val;