[2/2] hwmon: chipcap2: fix return path in cc2_request_alarm_irqs()

Message ID 20240207-chipcap2_init_vars-v1-2-08cafe43e20e@gmail.com
State New
Headers
Series hwmon: chipcap2: fix uninitialized symbols |

Commit Message

Javier Carrasco Feb. 7, 2024, 9:17 p.m. UTC
  The return path can be improved by returning upon first failure. The
current implementation would try to register the second interrupt even
if the first one failed, which is unnecessary.

Moreover, if no irqs are available, the return value should be zero
(the driver supports the use case with no interrupts). Currently the
initial value is unassigned and that may lead to returning an unknown
value if stack variables are not automatically set to zero and no irqs
were provided.

Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
Closes: https://lore.kernel.org/linux-hwmon/294e4634-89d4-415e-a723-b208d8770d7c@gmail.com/T/#t
Signed-off-by: Javier Carrasco <javier.carrasco.cruz@gmail.com>
---
 drivers/hwmon/chipcap2.c | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)
  

Comments

Guenter Roeck Feb. 7, 2024, 9:32 p.m. UTC | #1
On Wed, Feb 07, 2024 at 10:17:09PM +0100, Javier Carrasco wrote:
> The return path can be improved by returning upon first failure. The
> current implementation would try to register the second interrupt even
> if the first one failed, which is unnecessary.
> 
> Moreover, if no irqs are available, the return value should be zero
> (the driver supports the use case with no interrupts). Currently the
> initial value is unassigned and that may lead to returning an unknown
> value if stack variables are not automatically set to zero and no irqs
> were provided.
> 
> Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
> Closes: https://lore.kernel.org/linux-hwmon/294e4634-89d4-415e-a723-b208d8770d7c@gmail.com/T/#t
> Signed-off-by: Javier Carrasco <javier.carrasco.cruz@gmail.com>

Applied.

Thanks,
Guenter
  

Patch

diff --git a/drivers/hwmon/chipcap2.c b/drivers/hwmon/chipcap2.c
index 3b604fc5d8ae..6ccceae21f70 100644
--- a/drivers/hwmon/chipcap2.c
+++ b/drivers/hwmon/chipcap2.c
@@ -670,7 +670,7 @@  static int cc2_request_ready_irq(struct cc2_data *data, struct device *dev)
 
 static int cc2_request_alarm_irqs(struct cc2_data *data, struct device *dev)
 {
-	int ret;
+	int ret = 0;
 
 	data->irq_low = fwnode_irq_get_byname(dev_fwnode(dev), "low");
 	if (data->irq_low > 0) {
@@ -679,8 +679,10 @@  static int cc2_request_alarm_irqs(struct cc2_data *data, struct device *dev)
 						IRQF_ONESHOT |
 						IRQF_TRIGGER_RISING,
 						dev_name(dev), data);
-		if (!ret)
-			data->rh_alarm.low_alarm_visible = true;
+		if (ret)
+			return ret;
+
+		data->rh_alarm.low_alarm_visible = true;
 	}
 
 	data->irq_high = fwnode_irq_get_byname(dev_fwnode(dev), "high");
@@ -690,8 +692,10 @@  static int cc2_request_alarm_irqs(struct cc2_data *data, struct device *dev)
 						IRQF_ONESHOT |
 						IRQF_TRIGGER_RISING,
 						dev_name(dev), data);
-		if (!ret)
-			data->rh_alarm.high_alarm_visible = true;
+		if (ret)
+			return ret;
+
+		data->rh_alarm.high_alarm_visible = true;
 	}
 
 	return ret;