Skip to content

Commit ec8632d

Browse files
ananglkartben
authored andcommitted
drivers: adc_ad4130: Fix out-of-bounds accesses to channel_setup_cfg
Correct the size of the channel_setup_cfg array, as it should contain entries for all available channels (AD4130_MAX_CHANNELS), not for the available configuration slots (AD4130_MAX_SETUPS). Move also checking of the channel index to the very beginning of adc_ad4130_channel_setup(), to avoid potential writes to .live_cfg beyond the channel_setup_cfg array. Signed-off-by: Andrzej Głąbek <[email protected]>
1 parent 9b14f9b commit ec8632d

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

drivers/adc/adc_ad4130.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ struct ad4130_config {
229229
struct adc_ad4130_data {
230230
const struct device *dev;
231231
struct adc_context ctx;
232-
struct ad4130_channel_config channel_setup_cfg[AD4130_MAX_SETUPS];
232+
struct ad4130_channel_config channel_setup_cfg[AD4130_MAX_CHANNELS];
233233
uint8_t setup_cfg_slots;
234234
struct k_sem acquire_signal;
235235
uint16_t channels;
@@ -456,11 +456,6 @@ static int adc_ad4130_create_new_cfg(const struct device *dev, const struct adc_
456456
enum ad4130_ref_sel ref_source;
457457
enum ad4130_gain gain;
458458

459-
if (cfg->channel_id >= AD4130_MAX_CHANNELS) {
460-
LOG_ERR("Invalid channel (%u)", cfg->channel_id);
461-
return -EINVAL;
462-
}
463-
464459
if (cfg->acquisition_time != ADC_ACQ_TIME_DEFAULT) {
465460
LOG_ERR("invalid acquisition time %i", cfg->acquisition_time);
466461
return -EINVAL;
@@ -564,6 +559,11 @@ static int adc_ad4130_channel_setup(const struct device *dev, const struct adc_c
564559
int new_slot;
565560
int ret;
566561

562+
if (cfg->channel_id >= AD4130_MAX_CHANNELS) {
563+
LOG_ERR("Invalid channel (%u)", cfg->channel_id);
564+
return -EINVAL;
565+
}
566+
567567
data->channel_setup_cfg[cfg->channel_id].live_cfg = false;
568568

569569
ret = adc_ad4130_create_new_cfg(dev, cfg, &new_cfg);

0 commit comments

Comments
 (0)