diff mbox series

[2/2] firmware: arm_scmi: Fix pointers arithmetic in Base protocol

Message ID 20220530115237.277077-2-cristian.marussi@arm.com (mailing list archive)
State New, archived
Headers show
Series [1/2] firmware: arm_scmi: Remove unused local variables | expand

Commit Message

Cristian Marussi May 30, 2022, 11:52 a.m. UTC
Fix a possible undefined behaviour involving pointer arithmetic in Base
protocol scmi_base_implementation_list_get().

cppcheck complains with:

drivers/firmware/arm_scmi/base.c:190:19: warning: 't->rx.buf' is of type 'void *'. When using void pointers in calculations, the behaviour is undefined. [arithOperationsOnVoidPointer]
 list = t->rx.buf + sizeof(*num_ret);

Fixes: b6f20ff8bd94 ("firmware: arm_scmi: add common infrastructure and support for base protocol")
Cc: Sudeep Holla <sudeep.holla@arm.com>
Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
---
 drivers/firmware/arm_scmi/base.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Robin Murphy May 31, 2022, 11:02 a.m. UTC | #1
On 2022-05-30 12:52, Cristian Marussi wrote:
> Fix a possible undefined behaviour involving pointer arithmetic in Base
> protocol scmi_base_implementation_list_get().
> 
> cppcheck complains with:
> 
> drivers/firmware/arm_scmi/base.c:190:19: warning: 't->rx.buf' is of type 'void *'. When using void pointers in calculations, the behaviour is undefined. [arithOperationsOnVoidPointer]
>   list = t->rx.buf + sizeof(*num_ret);

Except we use GNU C, where it is well-defined[1]. We use void pointer 
arithmetic *all over* Linux, so there really isn't any valid argument 
that it could be problematic. If this was a common SCMI library intended 
to be portable then the patch would seem more reasonable, but in 
Linux-specific driver code it's just pointless churn.

Cheers,
Robin.

[1] https://gcc.gnu.org/onlinedocs/gcc/Pointer-Arith.html

> Fixes: b6f20ff8bd94 ("firmware: arm_scmi: add common infrastructure and support for base protocol")
> Cc: Sudeep Holla <sudeep.holla@arm.com>
> Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
> ---
>   drivers/firmware/arm_scmi/base.c | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/firmware/arm_scmi/base.c b/drivers/firmware/arm_scmi/base.c
> index 20fba7370f4e..6d6214d9e68c 100644
> --- a/drivers/firmware/arm_scmi/base.c
> +++ b/drivers/firmware/arm_scmi/base.c
> @@ -187,7 +187,7 @@ scmi_base_implementation_list_get(const struct scmi_protocol_handle *ph,
>   
>   	num_skip = t->tx.buf;
>   	num_ret = t->rx.buf;
> -	list = t->rx.buf + sizeof(*num_ret);
> +	list = ((u8 *)t->rx.buf) + sizeof(*num_ret);
>   
>   	do {
>   		size_t real_list_sz;
Cristian Marussi May 31, 2022, 11:36 a.m. UTC | #2
On Tue, May 31, 2022 at 12:02:53PM +0100, Robin Murphy wrote:
> On 2022-05-30 12:52, Cristian Marussi wrote:
> > Fix a possible undefined behaviour involving pointer arithmetic in Base
> > protocol scmi_base_implementation_list_get().
> > 
> > cppcheck complains with:
> > 
> > drivers/firmware/arm_scmi/base.c:190:19: warning: 't->rx.buf' is of type 'void *'. When using void pointers in calculations, the behaviour is undefined. [arithOperationsOnVoidPointer]
> >   list = t->rx.buf + sizeof(*num_ret);
> 
> Except we use GNU C, where it is well-defined[1]. We use void pointer
> arithmetic *all over* Linux, so there really isn't any valid argument that
> it could be problematic. If this was a common SCMI library intended to be
> portable then the patch would seem more reasonable, but in Linux-specific
> driver code it's just pointless churn.
> 

Hi Robin,

thanks for the correction, I'll drop this.

Thanks,
Cristian

> Cheers,
> Robin.
> 
> [1] https://gcc.gnu.org/onlinedocs/gcc/Pointer-Arith.html
> 
> > Fixes: b6f20ff8bd94 ("firmware: arm_scmi: add common infrastructure and support for base protocol")
> > Cc: Sudeep Holla <sudeep.holla@arm.com>
> > Signed-off-by: Cristian Marussi <cristian.marussi@arm.com>
> > ---
> >   drivers/firmware/arm_scmi/base.c | 2 +-
> >   1 file changed, 1 insertion(+), 1 deletion(-)
> > 
> > diff --git a/drivers/firmware/arm_scmi/base.c b/drivers/firmware/arm_scmi/base.c
> > index 20fba7370f4e..6d6214d9e68c 100644
> > --- a/drivers/firmware/arm_scmi/base.c
> > +++ b/drivers/firmware/arm_scmi/base.c
> > @@ -187,7 +187,7 @@ scmi_base_implementation_list_get(const struct scmi_protocol_handle *ph,
> >   	num_skip = t->tx.buf;
> >   	num_ret = t->rx.buf;
> > -	list = t->rx.buf + sizeof(*num_ret);
> > +	list = ((u8 *)t->rx.buf) + sizeof(*num_ret);
> >   	do {
> >   		size_t real_list_sz;
diff mbox series

Patch

diff --git a/drivers/firmware/arm_scmi/base.c b/drivers/firmware/arm_scmi/base.c
index 20fba7370f4e..6d6214d9e68c 100644
--- a/drivers/firmware/arm_scmi/base.c
+++ b/drivers/firmware/arm_scmi/base.c
@@ -187,7 +187,7 @@  scmi_base_implementation_list_get(const struct scmi_protocol_handle *ph,
 
 	num_skip = t->tx.buf;
 	num_ret = t->rx.buf;
-	list = t->rx.buf + sizeof(*num_ret);
+	list = ((u8 *)t->rx.buf) + sizeof(*num_ret);
 
 	do {
 		size_t real_list_sz;