Linux IIO development
 help / color / mirror / Atom feed
* [PATCH] iio: adc: MCP3564: fix calib_bias and calib_scale range checks
@ 2023-12-01  9:48 Javier Carrasco
  2023-12-04  9:28 ` Marius.Cristea
       [not found] ` <IA1PR11MB63958455F969F1506B3084769986A@IA1PR11MB6395.namprd11.prod.outlook.com>
  0 siblings, 2 replies; 3+ messages in thread
From: Javier Carrasco @ 2023-12-01  9:48 UTC (permalink / raw)
  To: Marius Cristea, Jonathan Cameron, Lars-Peter Clausen
  Cc: linux-iio, linux-kernel, Javier Carrasco

The current implementation uses the AND (&&) operator to check if the
value to write for IIO_CHAN_INFO_CALIBBIAS and IIO_CHAN_INFO_CALIBSCALE
is within the valid ranges.
The evaluated values are the lower and upper limits of the ranges,
so this operation always evaluates to false.

The OR (||) operator must be used instead.

Signed-off-by: Javier Carrasco <javier.carrasco.cruz@gmail.com>
---
This bug has been found when looking for use cases of IIO_AVAIL_RANGE
and the fix could not be tested with real hardware. The issue and its
soulution are rather simple, but still some testing and confirmation
would be welcome.
---
 drivers/iio/adc/mcp3564.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/iio/adc/mcp3564.c b/drivers/iio/adc/mcp3564.c
index e3f1de5fcc5a..d5fb1cae8aeb 100644
--- a/drivers/iio/adc/mcp3564.c
+++ b/drivers/iio/adc/mcp3564.c
@@ -918,7 +918,7 @@ static int mcp3564_write_raw(struct iio_dev *indio_dev,
 		mutex_unlock(&adc->lock);
 		return ret;
 	case IIO_CHAN_INFO_CALIBBIAS:
-		if (val < mcp3564_calib_bias[0] && val > mcp3564_calib_bias[2])
+		if (val < mcp3564_calib_bias[0] || val > mcp3564_calib_bias[2])
 			return -EINVAL;
 
 		mutex_lock(&adc->lock);
@@ -928,7 +928,7 @@ static int mcp3564_write_raw(struct iio_dev *indio_dev,
 		mutex_unlock(&adc->lock);
 		return ret;
 	case IIO_CHAN_INFO_CALIBSCALE:
-		if (val < mcp3564_calib_scale[0] && val > mcp3564_calib_scale[2])
+		if (val < mcp3564_calib_scale[0] || val > mcp3564_calib_scale[2])
 			return -EINVAL;
 
 		if (adc->calib_scale == val)

---
base-commit: 994d5c58e50e91bb02c7be4a91d5186292a895c8
change-id: 20231201-mcp3564_range_checks-221708838130

Best regards,
-- 
Javier Carrasco <javier.carrasco.cruz@gmail.com>


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] iio: adc: MCP3564: fix calib_bias and calib_scale range checks
  2023-12-01  9:48 [PATCH] iio: adc: MCP3564: fix calib_bias and calib_scale range checks Javier Carrasco
@ 2023-12-04  9:28 ` Marius.Cristea
       [not found] ` <IA1PR11MB63958455F969F1506B3084769986A@IA1PR11MB6395.namprd11.prod.outlook.com>
  1 sibling, 0 replies; 3+ messages in thread
From: Marius.Cristea @ 2023-12-04  9:28 UTC (permalink / raw)
  To: jic23, javier.carrasco.cruz, lars; +Cc: linux-kernel, linux-iio

Hi Javier Carrasco,

   Thank you for looking over the driver, for spotting and reporting
the bug. Your fix is OK.

Reviewed-by: Marius Cristea <marius.cristea@microchip.com>

Thanks,
Marius

On Fri, 2023-12-01 at 10:48 +0100, Javier Carrasco wrote:
> EXTERNAL EMAIL: Do not click links or open attachments unless you
> know the content is safe
> 
> The current implementation uses the AND (&&) operator to check if the
> value to write for IIO_CHAN_INFO_CALIBBIAS and
> IIO_CHAN_INFO_CALIBSCALE
> is within the valid ranges.
> The evaluated values are the lower and upper limits of the ranges,
> so this operation always evaluates to false.
> 
> The OR (||) operator must be used instead.
> 
> Signed-off-by: Javier Carrasco <javier.carrasco.cruz@gmail.com>
> ---
> This bug has been found when looking for use cases of IIO_AVAIL_RANGE
> and the fix could not be tested with real hardware. The issue and its
> soulution are rather simple, but still some testing and confirmation
> would be welcome.
> ---
>  drivers/iio/adc/mcp3564.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/iio/adc/mcp3564.c b/drivers/iio/adc/mcp3564.c
> index e3f1de5fcc5a..d5fb1cae8aeb 100644
> --- a/drivers/iio/adc/mcp3564.c
> +++ b/drivers/iio/adc/mcp3564.c
> @@ -918,7 +918,7 @@ static int mcp3564_write_raw(struct iio_dev
> *indio_dev,
>                 mutex_unlock(&adc->lock);
>                 return ret;
>         case IIO_CHAN_INFO_CALIBBIAS:
> -               if (val < mcp3564_calib_bias[0] && val >
> mcp3564_calib_bias[2])
> +               if (val < mcp3564_calib_bias[0] || val >
> mcp3564_calib_bias[2])
>                         return -EINVAL;
> 
>                 mutex_lock(&adc->lock);
> @@ -928,7 +928,7 @@ static int mcp3564_write_raw(struct iio_dev
> *indio_dev,
>                 mutex_unlock(&adc->lock);
>                 return ret;
>         case IIO_CHAN_INFO_CALIBSCALE:
> -               if (val < mcp3564_calib_scale[0] && val >
> mcp3564_calib_scale[2])
> +               if (val < mcp3564_calib_scale[0] || val >
> mcp3564_calib_scale[2])
>                         return -EINVAL;
> 
>                 if (adc->calib_scale == val)
> 
> ---
> base-commit: 994d5c58e50e91bb02c7be4a91d5186292a895c8
> change-id: 20231201-mcp3564_range_checks-221708838130
> 
> Best regards,
> --
> Javier Carrasco <javier.carrasco.cruz@gmail.com>
> 


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] iio: adc: MCP3564: fix calib_bias and calib_scale range checks
       [not found] ` <IA1PR11MB63958455F969F1506B3084769986A@IA1PR11MB6395.namprd11.prod.outlook.com>
@ 2023-12-04 13:52   ` Jonathan Cameron
  0 siblings, 0 replies; 3+ messages in thread
From: Jonathan Cameron @ 2023-12-04 13:52 UTC (permalink / raw)
  To: Marius.Cristea; +Cc: javier.carrasco.cruz, lars, linux-iio, linux-kernel

On Mon, 4 Dec 2023 09:23:45 +0000
<Marius.Cristea@microchip.com> wrote:

> Hi Javier Carrasco,
> 
>    Thank you for looking over the driver and spotting and reporting the bug. Your fix is OK.
> 
> Reviewed-by: Marius Cristea <marius.cristea@microchip.com>
> 
Applied to the fixes-togreg branch of iio.git and marked for stable.

Thanks,

Jonathan

> Thanks,
> Marius
> 
> ________________________________
> From: Javier Carrasco <javier.carrasco.cruz@gmail.com>
> Sent: Friday, December 1, 2023 11:48 AM
> To: Marius Cristea - M70257 <Marius.Cristea@microchip.com>; Jonathan Cameron <jic23@kernel.org>; Lars-Peter Clausen <lars@metafoo.de>
> Cc: linux-iio@vger.kernel.org <linux-iio@vger.kernel.org>; linux-kernel@vger.kernel.org <linux-kernel@vger.kernel.org>; Javier Carrasco <javier.carrasco.cruz@gmail.com>
> Subject: [PATCH] iio: adc: MCP3564: fix calib_bias and calib_scale range checks
> 
> EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
> 
> The current implementation uses the AND (&&) operator to check if the
> value to write for IIO_CHAN_INFO_CALIBBIAS and IIO_CHAN_INFO_CALIBSCALE
> is within the valid ranges.
> The evaluated values are the lower and upper limits of the ranges,
> so this operation always evaluates to false.
> 
> The OR (||) operator must be used instead.
> 
> Signed-off-by: Javier Carrasco <javier.carrasco.cruz@gmail.com>
> ---
> This bug has been found when looking for use cases of IIO_AVAIL_RANGE
> and the fix could not be tested with real hardware. The issue and its
> soulution are rather simple, but still some testing and confirmation
> would be welcome.
> ---
>  drivers/iio/adc/mcp3564.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/iio/adc/mcp3564.c b/drivers/iio/adc/mcp3564.c
> index e3f1de5fcc5a..d5fb1cae8aeb 100644
> --- a/drivers/iio/adc/mcp3564.c
> +++ b/drivers/iio/adc/mcp3564.c
> @@ -918,7 +918,7 @@ static int mcp3564_write_raw(struct iio_dev *indio_dev,
>                 mutex_unlock(&adc->lock);
>                 return ret;
>         case IIO_CHAN_INFO_CALIBBIAS:
> -               if (val < mcp3564_calib_bias[0] && val > mcp3564_calib_bias[2])
> +               if (val < mcp3564_calib_bias[0] || val > mcp3564_calib_bias[2])
>                         return -EINVAL;
> 
>                 mutex_lock(&adc->lock);
> @@ -928,7 +928,7 @@ static int mcp3564_write_raw(struct iio_dev *indio_dev,
>                 mutex_unlock(&adc->lock);
>                 return ret;
>         case IIO_CHAN_INFO_CALIBSCALE:
> -               if (val < mcp3564_calib_scale[0] && val > mcp3564_calib_scale[2])
> +               if (val < mcp3564_calib_scale[0] || val > mcp3564_calib_scale[2])
>                         return -EINVAL;
> 
>                 if (adc->calib_scale == val)
> 
> ---
> base-commit: 994d5c58e50e91bb02c7be4a91d5186292a895c8
> change-id: 20231201-mcp3564_range_checks-221708838130
> 
> Best regards,
> --
> Javier Carrasco <javier.carrasco.cruz@gmail.com>
> 


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-12-04 13:52 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-12-01  9:48 [PATCH] iio: adc: MCP3564: fix calib_bias and calib_scale range checks Javier Carrasco
2023-12-04  9:28 ` Marius.Cristea
     [not found] ` <IA1PR11MB63958455F969F1506B3084769986A@IA1PR11MB6395.namprd11.prod.outlook.com>
2023-12-04 13:52   ` Jonathan Cameron

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox