Skip to contentSkip to conversation

iio: fix infinite loop for gain_to_scaletables()

PATCH Applied Zicheng Qu 3 participants 3 messages Oct 31, 2024

Subsystems (2)
lore
Share & tools
lore

Zicheng Qu

author
#0 lore raw

PATCH 1/1 iio: fix infinite loop for gain_to_scaletables()

In iio_gts_build_avail_time_table(), it is checked that gts->num_itime is non-zero, but gts->num_itime is not checked in gain_to_scaletables(). The variable time_idx is initialized as gts->num_itime - 1. This implies that time_idx might initially be set to -1 (0 - 1 = -1). Consequently, using while (time_idx--) could lead to an infinite loop.

Fixes 38416c28e168 ("iio: light: Add gain-time-scale helpers")
---
 drivers/iio/industrialio-gts-helper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/industrialio-gts-helper.c +1 -1
index 59d7615c0f56..f3acd392f4fc 100644
@@ -205,7 +205,7 @@ static int gain_to_scaletables(struct iio_gts *gts, int **gains, int **scales)
 	memcpy(all_gains, gains[time_idx], gain_bytes);
 	new_idx = gts->num_hwgain;
 
-	while (time_idx--) {
+	while (time_idx-- > 0) {
 		for (j = 0; j < gts->num_hwgain; j++) {
 			int candidate = gains[time_idx][j];
 			int chk;
Signature · 1 line
2.34.1
maintainer Reviewed-by on v1 1/1
#1 lore raw

Thanks again Zicheng!

Quoted 18 lines · Zicheng Qu

In iio_gts_build_avail_time_table(), it is checked that gts->num_itime is non-zero, but gts->num_itime is not checked in gain_to_scaletables(). The variable time_idx is initialized as gts->num_itime - 1. This implies that time_idx might initially be set to -1 (0 - 1 = -1). Consequently, using while (time_idx--) could lead to an infinite loop.

Fixes 38416c28e168 ("iio: light: Add gain-time-scale helpers")
---
  drivers/iio/industrialio-gts-helper.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/industrialio-gts-helper.c +0 -0
index 59d7615c0f56..f3acd392f4fc 100644

drivers/iio/industrialio-gts-helper.c static int gain_to_scaletables(struct iio_gts *gts, int **gains, int **scales) · line 211

Quoted 8 lines
drivers/iio/industrialio-gts-helper.c +1 -1
@@ -205,7 +205,7 @@ static int gain_to_scaletables(struct iio_gts *gts, int **gains, int **scales)
  	memcpy(all_gains, gains[time_idx], gain_bytes);
  	new_idx = gts->num_hwgain;
 
-	while (time_idx--) {
+	while (time_idx-- > 0) {
  		for (j = 0; j < gts->num_hwgain; j++) {
  			int candidate = gains[time_idx][j];
  			int chk;

This, too, brings the question if supporting 0 times is worth.

At least this shows that it'd be nice to cover the "only times, no hw-gains" and "no times, only hw-gains" cases in the Kunit tests...

Anyways - Thanks!

Yours,
-- Matti

maintainer Applied
#2 lore raw
Quoted 32 lines · On Thu, 31 Oct 2024 09:25:16 +0200 Matti Vaittinen <[email protected]>…

Thanks again Zicheng!

Quoted 18 lines · Zicheng Qu

In iio_gts_build_avail_time_table(), it is checked that gts->num_itime is non-zero, but gts->num_itime is not checked in gain_to_scaletables(). The variable time_idx is initialized as gts->num_itime - 1. This implies that time_idx might initially be set to -1 (0 - 1 = -1). Consequently, using while (time_idx--) could lead to an infinite loop.

Fixes 38416c28e168 ("iio: light: Add gain-time-scale helpers")
---
  drivers/iio/industrialio-gts-helper.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
drivers/iio/industrialio-gts-helper.c +0 -0
index 59d7615c0f56..f3acd392f4fc 100644

drivers/iio/industrialio-gts-helper.c static int gain_to_scaletables(struct iio_gts *gts, int **gains, int **scales) · line 211

Quoted 8 lines
drivers/iio/industrialio-gts-helper.c +1 -1
@@ -205,7 +205,7 @@ static int gain_to_scaletables(struct iio_gts *gts, int **gains, int **scales)
  	memcpy(all_gains, gains[time_idx], gain_bytes);
  	new_idx = gts->num_hwgain;
 
-	while (time_idx--) {
+	while (time_idx-- > 0) {
  		for (j = 0; j < gts->num_hwgain; j++) {
  			int candidate = gains[time_idx][j];
  			int chk;  

This, too, brings the question if supporting 0 times is worth.

Quoted 6 lines

At least this shows that it'd be nice to cover the "only times, no hw-gains" and "no times, only hw-gains" cases in the Kunit tests...

Anyways - Thanks!

Applied

Quoted 5 lines

Yours,
-- Matti