VL53L4CD ULD : divide by 0 bug in VL53L4CD_GetResult
I am using VL53L4CX with VL53L4CD ULD driver v2.2.2, due to an I2C issue on my side the VL53L4CD_RdWord fails and the number_of_spad variable is set to 0. The divide by zero happen at the very end of VL53L4CD_GetResult.
I checked the function in ULD v2.2.3 and the number_of_spad value is still used for division without checks :
VL53L4CD_Error VL53L4CD_GetResult(
Dev_t dev,
VL53L4CD_ResultsData_t *p_result)
{
VL53L4CD_Error status = VL53L4CD_ERROR_NONE;
uint16_t temp_16;
uint8_t temp_8;
uint16_t raw_spads;
uint8_t status_rtn[24] = { 255, 255, 255, 5, 2, 4, 1, 7, 3,
0, 255, 255, 9, 13, 255, 255, 255, 255, 10, 6,
255, 255, 11, 12 };
status |= VL53L4CD_RdByte(dev, VL53L4CD_RESULT__RANGE_STATUS,
&temp_8);
temp_8 = temp_8 & (uint8_t)0x1F;
if (temp_8 < (uint8_t)24)
{
temp_8 = status_rtn[temp_8];
}
p_result->range_status = temp_8;
status |= VL53L4CD_RdWord(dev, VL53L4CD_RESULT__SPAD_NB,
&temp_16);
raw_spads=temp_16;
p_result->number_of_spad = temp_16 / (uint16_t) 256;
status |= VL53L4CD_RdWord(dev, VL53L4CD_RESULT__SIGNAL_RATE,
&temp_16);
p_result->signal_rate_kcps = (uint32_t)temp_16 * 8;
status |= VL53L4CD_RdWord(dev, VL53L4CD_RESULT__AMBIENT_RATE,
&temp_16);
p_result->ambient_rate_kcps = (uint32_t)temp_16 * 8;
status |= VL53L4CD_RdWord(dev, VL53L4CD_RESULT__SIGMA,
&temp_16);
p_result->sigma_mm = temp_16 / (uint16_t) 4;
status |= VL53L4CD_RdWord(dev, VL53L4CD_RESULT__DISTANCE,
&temp_16);
p_result->distance_mm = temp_16;
p_result->signal_per_spad_kcps = p_result->signal_rate_kcps *256
/(uint32_t) raw_spads;
p_result->ambient_per_spad_kcps = p_result->ambient_rate_kcps *256
/(uint32_t) raw_spads;
return status;
}
According to me, this lacks an if statement. Easy bug to spot and avoid anyway, but at least it is reported !
