icm42688: Fix divide by zero potential

There were code paths that could have lead to divide by zero given an
invalid scale setting for accel or gyro. In practice this should be an
invalid setup even before getting to these conversion functions. The
conversion functions now better show all valid values are accounted for
by using CODE_UNREACHABLE in the default case.

Signed-off-by: Tom Burdick <thomas.burdick@intel.com>
This commit is contained in:
Tom Burdick 2024-07-16 13:21:50 -05:00 committed by Anas Nashif
parent 04e3d0081b
commit 5474b611cb

View file

@ -417,7 +417,7 @@ int icm42688_read_all(const struct device *dev, uint8_t data[14]);
static inline void icm42688_accel_g(struct icm42688_cfg *cfg, int32_t in, int32_t *out_g, static inline void icm42688_accel_g(struct icm42688_cfg *cfg, int32_t in, int32_t *out_g,
uint32_t *out_ug) uint32_t *out_ug)
{ {
int32_t sensitivity = 0; /* value equivalent for 1g */ int32_t sensitivity;
switch (cfg->accel_fs) { switch (cfg->accel_fs) {
case ICM42688_DT_ACCEL_FS_2: case ICM42688_DT_ACCEL_FS_2:
@ -432,6 +432,8 @@ static inline void icm42688_accel_g(struct icm42688_cfg *cfg, int32_t in, int32_
case ICM42688_DT_ACCEL_FS_16: case ICM42688_DT_ACCEL_FS_16:
sensitivity = 2048; sensitivity = 2048;
break; break;
default:
CODE_UNREACHABLE;
} }
/* Whole g's */ /* Whole g's */
@ -452,7 +454,7 @@ static inline void icm42688_accel_g(struct icm42688_cfg *cfg, int32_t in, int32_
static inline void icm42688_gyro_dps(const struct icm42688_cfg *cfg, int32_t in, int32_t *out_dps, static inline void icm42688_gyro_dps(const struct icm42688_cfg *cfg, int32_t in, int32_t *out_dps,
uint32_t *out_udps) uint32_t *out_udps)
{ {
int64_t sensitivity = 0; /* value equivalent for 10x gyro reading deg/s */ int64_t sensitivity;
switch (cfg->gyro_fs) { switch (cfg->gyro_fs) {
case ICM42688_DT_GYRO_FS_2000: case ICM42688_DT_GYRO_FS_2000:
@ -479,6 +481,8 @@ static inline void icm42688_gyro_dps(const struct icm42688_cfg *cfg, int32_t in,
case ICM42688_DT_GYRO_FS_15_625: case ICM42688_DT_GYRO_FS_15_625:
sensitivity = 20972; sensitivity = 20972;
break; break;
default:
CODE_UNREACHABLE;
} }
int32_t in10 = in * 10; int32_t in10 = in * 10;