Skip to content

Commit 47fc9a1

Browse files
jilaypandyakartben
authored andcommitted
drivers: sensor: tdk: fix icm42688 division by zero
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: Jilay Pandya <[email protected]>
1 parent 73f1e7e commit 47fc9a1

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
@@ -506,7 +506,7 @@ static inline void icm42688_gyro_dps(const struct icm42688_cfg *cfg, int32_t in,
506506
static inline void icm42688_accel_ms(const struct icm42688_cfg *cfg, int32_t in, int32_t *out_ms,
507507
int32_t *out_ums)
508508
{
509-
int64_t sensitivity = 0; /* value equivalent for 1g */
509+
int64_t sensitivity;
510510

511511
switch (cfg->accel_fs) {
512512
case ICM42688_DT_ACCEL_FS_2:
@@ -521,6 +521,8 @@ static inline void icm42688_accel_ms(const struct icm42688_cfg *cfg, int32_t in,
521521
case ICM42688_DT_ACCEL_FS_16:
522522
sensitivity = 2048;
523523
break;
524+
default:
525+
CODE_UNREACHABLE;
524526
}
525527

526528
/* Convert to micrometers/s^2 */
@@ -544,7 +546,7 @@ static inline void icm42688_accel_ms(const struct icm42688_cfg *cfg, int32_t in,
544546
static inline void icm42688_gyro_rads(const struct icm42688_cfg *cfg, int32_t in, int32_t *out_rads,
545547
int32_t *out_urads)
546548
{
547-
int64_t sensitivity = 0; /* value equivalent for 10x gyro reading deg/s */
549+
int64_t sensitivity;
548550

549551
switch (cfg->gyro_fs) {
550552
case ICM42688_DT_GYRO_FS_2000:
@@ -571,6 +573,8 @@ static inline void icm42688_gyro_rads(const struct icm42688_cfg *cfg, int32_t in
571573
case ICM42688_DT_GYRO_FS_15_625:
572574
sensitivity = 20972;
573575
break;
576+
default:
577+
CODE_UNREACHABLE;
574578
}
575579

576580
int64_t in10_rads = (int64_t)in * SENSOR_PI * 10LL;

0 commit comments

Comments
 (0)