Skip to content

Commit 9227d69

Browse files
ukleineknunojsa
authored andcommitted
iio: adc: ad4630: Mask AD4630_REG_AVG to prevent UB
The value read from the AD4630_REG_AVG register is fine if it matches the documentation. Be a bit more prudent though and mask the value to the intended value. Still more as shifts bigger than the variable's width result in undefined behaviour. Also simplify var *= 1 << shift to var <<= shift . Signed-off-by: Uwe Kleine-König <u.kleine-koenig@baylibre.com>
1 parent 2e424f1 commit 9227d69

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

drivers/iio/adc/ad4630.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@
7777
#define AD4630_CLK_MODE_MSK GENMASK(5, 4)
7878
#define AD4630_DATA_RATE_MODE_MSK BIT(3)
7979
#define AD4630_OUT_DATA_MODE_MSK GENMASK(2, 0)
80+
/* AVG */
81+
#define AD4630_AVG_AVG_VAL GENMASK(4, 0)
8082
/* OFFSET */
8183
#define AD4630_REG_CHAN_OFFSET(ch) (AD4630_REG_OFFSET_X0_2 + 3 * (ch))
8284
/* HARDWARE_GAIN */
@@ -395,7 +397,7 @@ static int __ad4630_set_sampling_freq(const struct ad4630_state *st, unsigned in
395397
if (ret)
396398
return ret;
397399

398-
fetch_state.period *= 1 << avg;
400+
fetch_state.period <<= FIELD_GET(AD4630_AVG_AVG_VAL, avg);
399401
}
400402

401403
/*

0 commit comments

Comments
 (0)