linux-iio.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] iio: adc: mcp3911: fix sizeof() vs ARRAY_SIZE() bug
@ 2022-09-26  9:10 Dan Carpenter
  2022-09-26 11:18 ` Marcus Folkesson
  0 siblings, 1 reply; 3+ messages in thread
From: Dan Carpenter @ 2022-09-26  9:10 UTC (permalink / raw)
  To: Marcus Folkesson
  Cc: Kent Gustavsson, Jonathan Cameron, Lars-Peter Clausen,
	Andy Shevchenko, linux-iio, kernel-janitors

This code uses sizeof() instead of ARRAY_SIZE() so it reads beyond the
end of the mcp3911_osr_table[] array.

Fixes: 6d965885f4ea ("iio: adc: mcp3911: add support for oversampling ratio")
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
---
 drivers/iio/adc/mcp3911.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/iio/adc/mcp3911.c b/drivers/iio/adc/mcp3911.c
index b35fd2c9c3c0..015a9ffdb26a 100644
--- a/drivers/iio/adc/mcp3911.c
+++ b/drivers/iio/adc/mcp3911.c
@@ -248,7 +248,7 @@ static int mcp3911_write_raw(struct iio_dev *indio_dev,
 		break;
 
 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
-		for (int i = 0; i < sizeof(mcp3911_osr_table); i++) {
+		for (int i = 0; i < ARRAY_SIZE(mcp3911_osr_table); i++) {
 			if (val == mcp3911_osr_table[i]) {
 				val = FIELD_PREP(MCP3911_CONFIG_OSR, i);
 				ret = mcp3911_update(adc, MCP3911_REG_CONFIG, MCP3911_CONFIG_OSR,
-- 
2.35.1


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

* Re: [PATCH] iio: adc: mcp3911: fix sizeof() vs ARRAY_SIZE() bug
  2022-09-26  9:10 [PATCH] iio: adc: mcp3911: fix sizeof() vs ARRAY_SIZE() bug Dan Carpenter
@ 2022-09-26 11:18 ` Marcus Folkesson
  2022-10-02 12:20   ` Jonathan Cameron
  0 siblings, 1 reply; 3+ messages in thread
From: Marcus Folkesson @ 2022-09-26 11:18 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Kent Gustavsson, Jonathan Cameron, Lars-Peter Clausen,
	Andy Shevchenko, linux-iio, kernel-janitors

[-- Attachment #1: Type: text/plain, Size: 1174 bytes --]

Hi,

Good catch.
Too bad I missed it.

/Marcus

On Mon, Sep 26, 2022 at 12:10:37PM +0300, Dan Carpenter wrote:
> This code uses sizeof() instead of ARRAY_SIZE() so it reads beyond the
> end of the mcp3911_osr_table[] array.
> 
> Fixes: 6d965885f4ea ("iio: adc: mcp3911: add support for oversampling ratio")
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>

Reviewed-by: Marcus Folkesson <marcus.folkesson@gmail.com>

> ---
>  drivers/iio/adc/mcp3911.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/iio/adc/mcp3911.c b/drivers/iio/adc/mcp3911.c
> index b35fd2c9c3c0..015a9ffdb26a 100644
> --- a/drivers/iio/adc/mcp3911.c
> +++ b/drivers/iio/adc/mcp3911.c
> @@ -248,7 +248,7 @@ static int mcp3911_write_raw(struct iio_dev *indio_dev,
>  		break;
>  
>  	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
> -		for (int i = 0; i < sizeof(mcp3911_osr_table); i++) {
> +		for (int i = 0; i < ARRAY_SIZE(mcp3911_osr_table); i++) {
>  			if (val == mcp3911_osr_table[i]) {
>  				val = FIELD_PREP(MCP3911_CONFIG_OSR, i);
>  				ret = mcp3911_update(adc, MCP3911_REG_CONFIG, MCP3911_CONFIG_OSR,
> -- 
> 2.35.1
> 

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

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

* Re: [PATCH] iio: adc: mcp3911: fix sizeof() vs ARRAY_SIZE() bug
  2022-09-26 11:18 ` Marcus Folkesson
@ 2022-10-02 12:20   ` Jonathan Cameron
  0 siblings, 0 replies; 3+ messages in thread
From: Jonathan Cameron @ 2022-10-02 12:20 UTC (permalink / raw)
  To: Marcus Folkesson
  Cc: Dan Carpenter, Kent Gustavsson, Lars-Peter Clausen,
	Andy Shevchenko, linux-iio, kernel-janitors

On Mon, 26 Sep 2022 13:18:58 +0200
Marcus Folkesson <marcus.folkesson@gmail.com> wrote:

> Hi,
> 
> Good catch.
> Too bad I missed it.
> 
> /Marcus
> 
> On Mon, Sep 26, 2022 at 12:10:37PM +0300, Dan Carpenter wrote:
> > This code uses sizeof() instead of ARRAY_SIZE() so it reads beyond the
> > end of the mcp3911_osr_table[] array.
> > 
> > Fixes: 6d965885f4ea ("iio: adc: mcp3911: add support for oversampling ratio")
> > Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>  
> 
> Reviewed-by: Marcus Folkesson <marcus.folkesson@gmail.com>
Applied to the fixes-togreg branch of iio.git, but I'm not going to push that
tree out until Linus takes the char-misc-next pull request as otherwise I'll make
a mess of linux-next as the trees will effectively merge out of normal order.

Jonathan

> 
> > ---
> >  drivers/iio/adc/mcp3911.c | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/drivers/iio/adc/mcp3911.c b/drivers/iio/adc/mcp3911.c
> > index b35fd2c9c3c0..015a9ffdb26a 100644
> > --- a/drivers/iio/adc/mcp3911.c
> > +++ b/drivers/iio/adc/mcp3911.c
> > @@ -248,7 +248,7 @@ static int mcp3911_write_raw(struct iio_dev *indio_dev,
> >  		break;
> >  
> >  	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
> > -		for (int i = 0; i < sizeof(mcp3911_osr_table); i++) {
> > +		for (int i = 0; i < ARRAY_SIZE(mcp3911_osr_table); i++) {
> >  			if (val == mcp3911_osr_table[i]) {
> >  				val = FIELD_PREP(MCP3911_CONFIG_OSR, i);
> >  				ret = mcp3911_update(adc, MCP3911_REG_CONFIG, MCP3911_CONFIG_OSR,
> > -- 
> > 2.35.1
> >   


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

end of thread, other threads:[~2022-10-02 12:20 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-09-26  9:10 [PATCH] iio: adc: mcp3911: fix sizeof() vs ARRAY_SIZE() bug Dan Carpenter
2022-09-26 11:18 ` Marcus Folkesson
2022-10-02 12:20   ` Jonathan Cameron

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).