Browse Source

drivers: sensor: bmp581: fix potential null dereference

Move null check for 'dev' before dereferencing it to access dev->config.

This ensures the check is meaningful and avoids undefined behavior in case
of a null device pointer.

Signed-off-by: Gaetan Perrot <gaetan.perrot@spacecubics.com>
pull/92661/merge
Gaetan Perrot 1 week ago committed by Daniel DeGrasse
parent
commit
032c5301de
  1. 15
      drivers/sensor/bosch/bmp581/bmp581.c

15
drivers/sensor/bosch/bmp581/bmp581.c

@ -105,13 +105,14 @@ static int set_power_mode(enum bmp5_powermode powermode, const struct device *de
static int get_power_mode(enum bmp5_powermode *powermode, const struct device *dev) static int get_power_mode(enum bmp5_powermode *powermode, const struct device *dev)
{ {
struct bmp581_config *conf = (struct bmp581_config *)dev->config; struct bmp581_config *conf;
int ret = BMP5_OK; int ret = BMP5_OK;
CHECKIF(powermode == NULL || dev == NULL) { CHECKIF(powermode == NULL || dev == NULL) {
return -EINVAL; return -EINVAL;
} }
conf = (struct bmp581_config *)dev->config;
uint8_t reg = 0; uint8_t reg = 0;
uint8_t raw_power_mode = 0; uint8_t raw_power_mode = 0;
@ -186,23 +187,27 @@ static int power_up_check(const struct device *dev)
static int get_interrupt_status(uint8_t *int_status, const struct device *dev) static int get_interrupt_status(uint8_t *int_status, const struct device *dev)
{ {
struct bmp581_config *conf = (struct bmp581_config *)dev->config; struct bmp581_config *conf;
CHECKIF(int_status == NULL || dev == NULL) { CHECKIF(int_status == NULL || dev == NULL) {
return -EINVAL; return -EINVAL;
} }
conf = (struct bmp581_config *)dev->config;
return i2c_reg_read_byte_dt(&conf->i2c, BMP5_REG_INT_STATUS, int_status); return i2c_reg_read_byte_dt(&conf->i2c, BMP5_REG_INT_STATUS, int_status);
} }
static int get_nvm_status(uint8_t *nvm_status, const struct device *dev) static int get_nvm_status(uint8_t *nvm_status, const struct device *dev)
{ {
struct bmp581_config *conf = (struct bmp581_config *)dev->config; struct bmp581_config *conf;
CHECKIF(nvm_status == NULL || dev == NULL) { CHECKIF(nvm_status == NULL || dev == NULL) {
return -EINVAL; return -EINVAL;
} }
conf = (struct bmp581_config *)dev->config;
return i2c_reg_read_byte_dt(&conf->i2c, BMP5_REG_STATUS, nvm_status); return i2c_reg_read_byte_dt(&conf->i2c, BMP5_REG_STATUS, nvm_status);
} }
@ -231,7 +236,7 @@ static int validate_chip_id(struct bmp581_data *drv)
static int get_osr_odr_press_config(struct bmp581_osr_odr_press_config *osr_odr_press_cfg, static int get_osr_odr_press_config(struct bmp581_osr_odr_press_config *osr_odr_press_cfg,
const struct device *dev) const struct device *dev)
{ {
struct bmp581_config *conf = (struct bmp581_config *)dev->config; struct bmp581_config *conf;
/* Variable to store the function result */ /* Variable to store the function result */
int8_t rslt = 0; int8_t rslt = 0;
@ -243,6 +248,8 @@ static int get_osr_odr_press_config(struct bmp581_osr_odr_press_config *osr_odr_
return -EINVAL; return -EINVAL;
} }
conf = (struct bmp581_config *)dev->config;
/* Get OSR and ODR configuration in burst read */ /* Get OSR and ODR configuration in burst read */
rslt = i2c_burst_read_dt(&conf->i2c, BMP5_REG_OSR_CONFIG, reg_data, 2); rslt = i2c_burst_read_dt(&conf->i2c, BMP5_REG_OSR_CONFIG, reg_data, 2);

Loading…
Cancel
Save