[PATCH 1/2] staging: iio: ad9834: Correct phase range check

Zicheng Qu posted 2 patches 2 weeks, 4 days ago
[PATCH 1/2] staging: iio: ad9834: Correct phase range check
Posted by Zicheng Qu 2 weeks, 4 days ago
The phase register for the AD9834 is 12 bits, which means the valid
phase values range from 0 to 4095 (2^12 - 1). The current check uses a
greater-than condition with BIT(12), which equals 4096. This allows an
invalid phase value of 4096 to pass.

This patch corrects the check to use greater-than-or-equal-to, ensuring
that only phase values within the valid range are accepted.

Fixes: f1d05b5f68cb ("Staging: iio: Prefer using the BIT macro")
Cc: <stable@vger.kernel.org>
Signed-off-by: Zicheng Qu <quzicheng@huawei.com>
---
 drivers/staging/iio/frequency/ad9834.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/staging/iio/frequency/ad9834.c b/drivers/staging/iio/frequency/ad9834.c
index 47e7d7e6d920..6e99e008c5f4 100644
--- a/drivers/staging/iio/frequency/ad9834.c
+++ b/drivers/staging/iio/frequency/ad9834.c
@@ -131,7 +131,7 @@ static int ad9834_write_frequency(struct ad9834_state *st,
 static int ad9834_write_phase(struct ad9834_state *st,
 			      unsigned long addr, unsigned long phase)
 {
-	if (phase > BIT(AD9834_PHASE_BITS))
+	if (phase >= BIT(AD9834_PHASE_BITS))
 		return -EINVAL;
 	st->data = cpu_to_be16(addr | phase);
 
-- 
2.34.1
Re: [PATCH 1/2] staging: iio: ad9834: Correct phase range check
Posted by Dan Carpenter 2 weeks, 4 days ago
On Tue, Nov 05, 2024 at 02:03:58PM +0000, Zicheng Qu wrote:
> The phase register for the AD9834 is 12 bits, which means the valid
> phase values range from 0 to 4095 (2^12 - 1). The current check uses a
> greater-than condition with BIT(12), which equals 4096. This allows an
> invalid phase value of 4096 to pass.
> 

I tried to figure out how this looks like to the user but I didn't
figure it out right away.  Please add this information to the commit
message.

> This patch corrects the check to use greater-than-or-equal-to, ensuring
> that only phase values within the valid range are accepted.
> 
> Fixes: f1d05b5f68cb ("Staging: iio: Prefer using the BIT macro")

The Fixes tag is wrong.  It should be:

Fixes: 12b9d5bf76bf ("Staging: IIO: DDS: AD9833 / AD9834 driver")

regards,
dan carpenter