From a2190cc92bc1c3ebc2a4f20ad69cafb0afc2780b Mon Sep 17 00:00:00 2001 From: Avi Green Date: Sun, 6 Feb 2022 11:09:24 +0200 Subject: [PATCH] bmi160: bmi160_bus union bugfix bmi160.c module defines DT_DRV_COMPAT, but bmi160_trigger.c doesn't. This causes a catastrophic chain of events. The bmi160.c module includes bmi160.h, in which the macro DT_ANY_INST_ON_BUS_STATUS_OKAY affects the size of bmi160_bus union. So bmi160.c defines a bmi160_cfg struct which contains that union. Now, in bmi160_trigger_init we get a pointer to that config struct. The fact that this module now includes bmi160.h without DT_DRV_COMPAT, causes it to think the union is empty. That doesn't cause compilation error, just undefined behaviour, In which you address an empty struct fields. In general, I suggest that someone makes sure it doesn't happen in other drivers as well. The problem presented here is general, meaning that if an h file assumes someone defined DT_DRV_COMPAT before and it doesn't, it may lead to some weird behaviour, like the one described. Signed-off-by: Avi Green --- drivers/sensor/bmi160/bmi160.c | 2 -- drivers/sensor/bmi160/bmi160.h | 2 ++ 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/sensor/bmi160/bmi160.c b/drivers/sensor/bmi160/bmi160.c index 8eb03ba8291..4b8e76cf226 100644 --- a/drivers/sensor/bmi160/bmi160.c +++ b/drivers/sensor/bmi160/bmi160.c @@ -8,8 +8,6 @@ * http://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BMI160-DS000-07.pdf */ -#define DT_DRV_COMPAT bosch_bmi160 - #include #include #include diff --git a/drivers/sensor/bmi160/bmi160.h b/drivers/sensor/bmi160/bmi160.h index cfa5ade6f5f..d609f547748 100644 --- a/drivers/sensor/bmi160/bmi160.h +++ b/drivers/sensor/bmi160/bmi160.h @@ -8,6 +8,8 @@ #ifndef ZEPHYR_DRIVERS_SENSOR_BMI160_BMI160_H_ #define ZEPHYR_DRIVERS_SENSOR_BMI160_BMI160_H_ +#define DT_DRV_COMPAT bosch_bmi160 + #include #include #include