Skip to content

Commit 5474b61

Browse files
teburdnashif
authored andcommitted
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 <[email protected]>
1 parent 04e3d00 commit 5474b61

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

drivers/sensor/tdk/icm42688/icm42688.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ int icm42688_read_all(const struct device *dev, uint8_t data[14]);
417417
static inline void icm42688_accel_g(struct icm42688_cfg *cfg, int32_t in, int32_t *out_g,
418418
uint32_t *out_ug)
419419
{
420-
int32_t sensitivity = 0; /* value equivalent for 1g */
420+
int32_t sensitivity;
421421

422422
switch (cfg->accel_fs) {
423423
case ICM42688_DT_ACCEL_FS_2:
@@ -432,6 +432,8 @@ static inline void icm42688_accel_g(struct icm42688_cfg *cfg, int32_t in, int32_
432432
case ICM42688_DT_ACCEL_FS_16:
433433
sensitivity = 2048;
434434
break;
435+
default:
436+
CODE_UNREACHABLE;
435437
}
436438

437439
/* Whole g's */
@@ -452,7 +454,7 @@ static inline void icm42688_accel_g(struct icm42688_cfg *cfg, int32_t in, int32_
452454
static inline void icm42688_gyro_dps(const struct icm42688_cfg *cfg, int32_t in, int32_t *out_dps,
453455
uint32_t *out_udps)
454456
{
455-
int64_t sensitivity = 0; /* value equivalent for 10x gyro reading deg/s */
457+
int64_t sensitivity;
456458

457459
switch (cfg->gyro_fs) {
458460
case ICM42688_DT_GYRO_FS_2000:
@@ -479,6 +481,8 @@ static inline void icm42688_gyro_dps(const struct icm42688_cfg *cfg, int32_t in,
479481
case ICM42688_DT_GYRO_FS_15_625:
480482
sensitivity = 20972;
481483
break;
484+
default:
485+
CODE_UNREACHABLE;
482486
}
483487

484488
int32_t in10 = in * 10;

0 commit comments

Comments
 (0)