diff mbox series

[2/2,next] media: venus: hfi_cmds: Use struct_size() helper

Message ID fd52d6ddce285474615e4bd96931ab12a0da8199.1684278538.git.gustavoars@kernel.org (mailing list archive)
State Not Applicable
Headers show
Series media: venus: hfi_cmds: Replace one-element array with flexible-array member | expand

Commit Message

Gustavo A. R. Silva May 16, 2023, 11:14 p.m. UTC
Prefer struct_size() over open-coded versions of idiom:

sizeof(struct-with-flex-array) + sizeof(typeof-flex-array-elements) * count

where count is the max number of items the flexible array is supposed to
contain.

Link: https://github.com/KSPP/linux/issues/160
Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
---
 drivers/media/platform/qcom/venus/hfi_cmds.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

Comments

Kees Cook May 17, 2023, 7:12 p.m. UTC | #1
On Tue, May 16, 2023 at 05:14:49PM -0600, Gustavo A. R. Silva wrote:
> Prefer struct_size() over open-coded versions of idiom:
> 
> sizeof(struct-with-flex-array) + sizeof(typeof-flex-array-elements) * count
> 
> where count is the max number of items the flexible array is supposed to
> contain.
> 
> Link: https://github.com/KSPP/linux/issues/160
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>

Reviewed-by: Kees Cook <keescook@chromium.org>
Vikash Garodia May 25, 2023, 12:20 p.m. UTC | #2
On 5/17/2023 4:44 AM, Gustavo A. R. Silva wrote:
> Prefer struct_size() over open-coded versions of idiom:
> 
> sizeof(struct-with-flex-array) + sizeof(typeof-flex-array-elements) * count
> 
> where count is the max number of items the flexible array is supposed to
> contain.
> 
> Link: https://github.com/KSPP/linux/issues/160
> Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>

The patch looks good. As stated in previous patch, lets combine this into a
single series.

> ---
>  drivers/media/platform/qcom/venus/hfi_cmds.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/media/platform/qcom/venus/hfi_cmds.c b/drivers/media/platform/qcom/venus/hfi_cmds.c
> index 21d1b3c90dc0..3f74d518ad08 100644
> --- a/drivers/media/platform/qcom/venus/hfi_cmds.c
> +++ b/drivers/media/platform/qcom/venus/hfi_cmds.c
> @@ -209,8 +209,8 @@ int pkt_session_set_buffers(struct hfi_session_set_buffers_pkt *pkt,
>  		}
>  	} else {
>  		pkt->extradata_size = 0;
> -		pkt->shdr.hdr.size = sizeof(*pkt) +
> -			bd->num_buffers * sizeof(u32);
> +		pkt->shdr.hdr.size = struct_size(pkt, buffer_info,
> +						 bd->num_buffers);
>  		for (i = 0; i < pkt->num_buffers; i++)
>  			pkt->buffer_info[i] = bd->device_addr;
>  	}
> @@ -251,8 +251,8 @@ int pkt_session_unset_buffers(struct hfi_session_release_buffer_pkt *pkt,
>  
>  		pkt->extradata_size = 0;
>  		pkt->shdr.hdr.size =
> -				sizeof(struct hfi_session_set_buffers_pkt) +
> -				bd->num_buffers * sizeof(u32);
> +			struct_size((struct hfi_session_set_buffers_pkt *)0,
> +				    buffer_info, bd->num_buffers);
>  	}
>  
>  	pkt->response_req = bd->response_required;
diff mbox series

Patch

diff --git a/drivers/media/platform/qcom/venus/hfi_cmds.c b/drivers/media/platform/qcom/venus/hfi_cmds.c
index 21d1b3c90dc0..3f74d518ad08 100644
--- a/drivers/media/platform/qcom/venus/hfi_cmds.c
+++ b/drivers/media/platform/qcom/venus/hfi_cmds.c
@@ -209,8 +209,8 @@  int pkt_session_set_buffers(struct hfi_session_set_buffers_pkt *pkt,
 		}
 	} else {
 		pkt->extradata_size = 0;
-		pkt->shdr.hdr.size = sizeof(*pkt) +
-			bd->num_buffers * sizeof(u32);
+		pkt->shdr.hdr.size = struct_size(pkt, buffer_info,
+						 bd->num_buffers);
 		for (i = 0; i < pkt->num_buffers; i++)
 			pkt->buffer_info[i] = bd->device_addr;
 	}
@@ -251,8 +251,8 @@  int pkt_session_unset_buffers(struct hfi_session_release_buffer_pkt *pkt,
 
 		pkt->extradata_size = 0;
 		pkt->shdr.hdr.size =
-				sizeof(struct hfi_session_set_buffers_pkt) +
-				bd->num_buffers * sizeof(u32);
+			struct_size((struct hfi_session_set_buffers_pkt *)0,
+				    buffer_info, bd->num_buffers);
 	}
 
 	pkt->response_req = bd->response_required;