From a2db594eb5686a35cdf3fc483cb581a64babbaee Mon Sep 17 00:00:00 2001 From: Virgile Robles Date: Sun, 20 Oct 2024 07:53:52 +0200 Subject: [PATCH 1/2] drivers/dht: fix null deref with saul --- drivers/dht/dht.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/drivers/dht/dht.c b/drivers/dht/dht.c index 5f26a0b533a8..437e7ae4c023 100644 --- a/drivers/dht/dht.c +++ b/drivers/dht/dht.c @@ -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; } From 7c68f00cd1a86502b5fcd5f0943c3baaaf275895 Mon Sep 17 00:00:00 2001 From: Virgile Robles Date: Mon, 21 Oct 2024 15:13:23 +0200 Subject: [PATCH 2/2] drivers/dht: test and doc --- drivers/include/dht.h | 8 +++++--- tests/drivers/dht/main.c | 10 +++++++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/drivers/include/dht.h b/drivers/include/dht.h index c190b2dd3c4c..d02b4367b02d 100644 --- a/drivers/include/dht.h +++ b/drivers/include/dht.h @@ -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 diff --git a/tests/drivers/dht/main.c b/tests/drivers/dht/main.c index 8500a6f7dba2..ce70d89a56c4 100644 --- a/tests/drivers/dht/main.c +++ b/tests/drivers/dht/main.c @@ -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; }