Skip to content

Commit 34f5f8d

Browse files
sudarsan-22kartben
authored andcommitted
drivers: adc: Prevent overflow in max1125x_read_sample
Fix potential integer overflow caused by unsafe shift when computing ADC mid-scale offset. Applies resolution bounds and uses unsigned shift to avoid undefined behavior. Fixes: CID 487740 Signed-off-by: Sudarsan N <sudarsansamy2002@gmail.com>
1 parent 1fbb4e8 commit 34f5f8d

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

drivers/adc/adc_max1125x.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,9 +403,17 @@ static int max1125x_read_sample(const struct device *dev)
403403
* the available input range is limited to the minimum or maximum
404404
* data value.
405405
*/
406+
407+
if (config->resolution > 24 || config->resolution < 1) {
408+
LOG_ERR("Unsupported ADC resolution: %u", config->resolution);
409+
return -EINVAL;
410+
}
411+
406412
is_positive = buffer_rx[(config->resolution / 8)] >> 7;
413+
407414
if (is_positive) {
408-
*data->buffer++ = sys_get_be24(buffer_rx) - (1 << (config->resolution - 1));
415+
/* Ensure left shift is done using unsigned literal to avoid overflow. */
416+
*data->buffer++ = sys_get_be24(buffer_rx) - (1U << (config->resolution - 1));
409417
} else {
410418
*data->buffer++ = sys_get_be24(buffer_rx + 1);
411419
}

0 commit comments

Comments
 (0)