Skip to content

Commit

Permalink
drivers/sensor: lps22hh: Fix int32 overflow in the val2 part
Browse files Browse the repository at this point in the history
The val2 calculation was done using (1000000 / 40960) as
multiplying factor, which was sometimes leading to a
int32 overflow. So, let's use the equivalent (but smaller)
(3125 / 128).

Fix #38090

Signed-off-by: Armando Visconti <armando.visconti@st.com>
  • Loading branch information
avisconti authored and cfriedt committed Sep 6, 2021
1 parent 5591357 commit c3050a5
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion drivers/sensor/lps22hh/lps22hh.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ static inline void lps22hh_press_convert(struct sensor_value *val,
/* Also convert hPa into kPa */

val->val1 = press_tmp / 40960;
val->val2 = (press_tmp % 40960) * 1000000 / 40960;

/* For the decimal part use (3125 / 128) as a factor instead of
* (1000000 / 40960) to avoid int32 overflow
*/
val->val2 = (press_tmp % 40960) * 3125 / 128;
}

static inline void lps22hh_temp_convert(struct sensor_value *val,
Expand Down

0 comments on commit c3050a5

Please sign in to comment.