Skip to content

Commit

Permalink
Merge pull request #20927 from Firobe/dht-fix
Browse files Browse the repository at this point in the history
drivers/dht: fix null deref with saul
  • Loading branch information
mguetschow authored Oct 21, 2024
2 parents c48247f + 7c68f00 commit e5c185b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 6 deletions.
8 changes: 6 additions & 2 deletions drivers/dht/dht.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,12 @@ int dht_read(dht_t *dev, int16_t *temp, int16_t *hum)
return ret;
}

*hum = dev->last_val.humidity;
*temp = dev->last_val.temperature;
if (hum != NULL) {
*hum = dev->last_val.humidity;
}
if (temp != NULL) {
*temp = dev->last_val.temperature;
}

return 0;
}
8 changes: 5 additions & 3 deletions drivers/include/dht.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,16 @@ typedef struct {
int dht_init(dht_t *dev, const dht_params_t *params);

/**
* @brief get a new temperature and humidity value from the device
* @brief get a new temperature and/or humidity value from the device
*
* @note if reading fails or checksum is invalid, no new values will be
* written into the result values
*
* @param[in] dev device descriptor of a DHT device
* @param[out] temp temperature value [in °C * 10^-1]
* @param[out] hum relative humidity value [in percent * 10^-1]
* @param[out] temp temperature value [in °C * 10^-1],
* may be NULL if not needed
* @param[out] hum relative humidity value [in percent * 10^-1],
* may be NULL if not needed
*
* @retval 0 Success
* @retval -ENODEV The sensor did not respond to the transmission of a
Expand Down
10 changes: 9 additions & 1 deletion tests/drivers/dht/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@ int main(void)
ztimer_sleep(ZTIMER_USEC, DELAY);

if (dht_read(&dev, &temp, &hum) != DHT_OK) {
puts("Error reading values");
puts("Error reading both values");
continue;
}
if (dht_read(&dev, &temp, NULL) != DHT_OK) {
puts("Error reading just temperature");
continue;
}
if (dht_read(&dev, NULL, &hum) != DHT_OK) {
puts("Error reading just humidity");
continue;
}

Expand Down

0 comments on commit e5c185b

Please sign in to comment.