diff mbox series

hwmon: (ltc2991) re-order conditions to fix off by one bug

Message ID Zoa9Y_UMY4_ROfhF@stanley.mountain (mailing list archive)
State Accepted
Headers show
Series hwmon: (ltc2991) re-order conditions to fix off by one bug | expand

Commit Message

Dan Carpenter July 4, 2024, 3:18 p.m. UTC
LTC2991_T_INT_CH_NR is 4.  The st->temp_en[] array has LTC2991_MAX_CHANNEL
(4) elements.  Thus if "channel" is equal to LTC2991_T_INT_CH_NR then we
have read one element beyond the end of the array.  Flip the conditions
around so that we check if "channel" is valid before using it as an array
index.

Fixes: 2b9ea4262ae9 ("hwmon: Add driver for ltc2991")
Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
---
 drivers/hwmon/ltc2991.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Guenter Roeck July 5, 2024, 1:15 a.m. UTC | #1
On Thu, Jul 04, 2024 at 10:18:59AM -0500, Dan Carpenter wrote:
> LTC2991_T_INT_CH_NR is 4.  The st->temp_en[] array has LTC2991_MAX_CHANNEL
> (4) elements.  Thus if "channel" is equal to LTC2991_T_INT_CH_NR then we
> have read one element beyond the end of the array.  Flip the conditions
> around so that we check if "channel" is valid before using it as an array
> index.
> 
> Fixes: 2b9ea4262ae9 ("hwmon: Add driver for ltc2991")
> Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>

Applied.

Thanks,
Guenter
diff mbox series

Patch

diff --git a/drivers/hwmon/ltc2991.c b/drivers/hwmon/ltc2991.c
index a01f887392f9..573cd8f5721b 100644
--- a/drivers/hwmon/ltc2991.c
+++ b/drivers/hwmon/ltc2991.c
@@ -225,8 +225,8 @@  static umode_t ltc2991_is_visible(const void *data,
 	case hwmon_temp:
 		switch (attr) {
 		case hwmon_temp_input:
-			if (st->temp_en[channel] ||
-			    channel == LTC2991_T_INT_CH_NR)
+			if (channel == LTC2991_T_INT_CH_NR ||
+			    st->temp_en[channel])
 				return 0444;
 			break;
 		}