drivers/adxl372: Fix computation of sensor value
Sensor value computation was creating a 64-bit integer value, passing that to 'abs' and assigning that to an int32_t result and then sanity checking the result. If the computation goes badly wrong, then the range reduction of 64-bit to 32-bit values could generate a falsely in-range value. Instead, perform the computation in 64-bits, range check that value and then assign to a 32-bit variable. Signed-off-by: Keith Packard <keithp@keithp.com>
This commit is contained in:
parent
0970cda906
commit
7c6fe0735d
1 changed files with 5 additions and 2 deletions
|
@ -643,16 +643,19 @@ static int adxl372_attr_set_thresh(const struct device *dev,
|
|||
{
|
||||
const struct adxl372_dev_config *cfg = dev->config;
|
||||
struct adxl372_activity_threshold threshold;
|
||||
int64_t llvalue;
|
||||
int32_t value;
|
||||
int64_t micro_ms2 = val->val1 * 1000000LL + val->val2;
|
||||
uint8_t reg;
|
||||
|
||||
value = abs((micro_ms2 * 10) / SENSOR_G);
|
||||
llvalue = llabs((micro_ms2 * 10) / SENSOR_G);
|
||||
|
||||
if (value > 2047) {
|
||||
if (llvalue > 2047) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
value = (int32_t) llvalue;
|
||||
|
||||
threshold.thresh = value;
|
||||
threshold.enable = cfg->activity_th.enable;
|
||||
threshold.referenced = cfg->activity_th.referenced;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue