From 459b4434b18d83505e1acf28722b5955258ea44d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20=C5=81yszczek?= Date: Sat, 2 Mar 2024 17:23:33 +0100 Subject: [PATCH] drivers/analog/hx711.c: fix averaging on negative values MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ptype of ((int32_t)0 * (uint32_t)0) is (unsigned). "i" counter was declared unsigned to not make comparision with "unsigned char" in for loop. This resulted in calculation in average to be implicitly casted to unsigned, and when negative number was added to it, it turned into huge value. Change type of "i" to signed, and just cast (unsigned char) to (int) to fix this. Signed-off-by: Michał Łyszczek --- drivers/analog/hx711.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/analog/hx711.c b/drivers/analog/hx711.c index 90f04178be..353684dc66 100644 --- a/drivers/analog/hx711.c +++ b/drivers/analog/hx711.c @@ -648,7 +648,7 @@ static ssize_t hx711_read(FAR struct file *filep, int ret; int32_t value; /* 24bit value from hx711 will be stored here */ int32_t average; - unsigned i; + int i; value = 0; dev = filep->f_inode->i_private; @@ -671,7 +671,7 @@ static ssize_t hx711_read(FAR struct file *filep, return ret; } - for (i = 1; i <= dev->average; i++) + for (i = 1; i <= (int)dev->average; i++) { value = hx711_single_read(dev); if (value == INT32_MIN)