Skip to content

Commit e661a55

Browse files
JordanYateskartben
authored andcommitted
sensor: bme280: clamp humidity to non-negative
Clamp the returned humidity to a non-negative value. This ensure that the return value is always within the expected range, even if the calibration parameters are read out incorrectly or corrupted. This check is applied in the example Bosch driver for the float conversion case, so there are situations that warrant it. Signed-off-by: Jordan Yates <jordan@embeint.com>
1 parent 2959750 commit e661a55

File tree

1 file changed

+1
-0
lines changed

1 file changed

+1
-0
lines changed

drivers/sensor/bosch/bme280/bme280.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ static uint32_t bme280_compensate_humidity(struct bme280_data *data,
126126
h = (h - (((((h >> 15) * (h >> 15)) >> 7) *
127127
((int32_t)data->dig_h1)) >> 4));
128128
h = (h > 419430400 ? 419430400 : h);
129+
h = (h < 0 ? 0 : h);
129130

130131
return (uint32_t)(h >> 12);
131132
}

0 commit comments

Comments
 (0)