02-09-2021 04:13 PM
In trigger_foc(), https://github.com/BoschSensortec/BMI160_driver/blob/master/bmi160.c#L6313 , the foc_status variable is not initalized with a value. Since the call to get_foc_status() can return without writing to foc_status, it will contain garbage when it is evaluated at https://github.com/BoschSensortec/BMI160_driver/blob/master/bmi160.c#L6329
Solved! Go to Solution.
02-10-2021 03:22 AM
Hello arurke,
First of all, thanks for posting 🙂
Since get_foc_status should get foc status, we don't need to initialize for foc_status variable.
And it gets foc status in get_foc_status function, it gets status value from BMI160_STATUS_ADDR address.
https://github.com/BoschSensortec/BMI160_driver/blob/master/bmi160.c#L6264
I don't think there is some bug, could you elaborate if you still have some quetsions?
Thanks,
02-10-2021 10:13 AM
> Since get_foc_status should get foc status, we don't need to initialize for foc_status variable.
get_foc_status() does not guarantee that it does get foc_status: If the condition at line L6271, https://github.com/BoschSensortec/BMI160_driver/blob/master/bmi160.c#L6271, evaluates to false, the function will return without doing anything to the foc_status variable which thus still will contain garbage.
02-10-2021 06:12 PM
Hello arurke,
I understood. You mean if get_foc_status returns false, trigger_foc returns false without anything, right?
Yes, that's right. You can make while loop for trigger_foc, but we don't do that because some customer want to check the error code and handle differet way.
Therefore, if get_foc_status returns false, customer can notice that the application doesn't perform FOC properly.
Thanks,
02-10-2021 07:54 PM - edited 02-10-2021 08:02 PM
Not sure if I understood your follow-up... Just to be very clear about the issue:
1. Line 6316: foc_status is declared but not initialized
2. Line 6326: get_foc_status() is called.
3. Line 6271: The read fails so rslt is not BMI160_OK
4. Line 6277: get_foc_status() returns. foc_status has not been assigned any value(!)
5. Line 6327: foc_status is used in comparison. This is undefined behavior(!) since foc_status has not been initialized