lkml.org 
[lkml]   [2019]   [Jan]   [11]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
SubjectRe: [PATCH] scsi: advansys: use struct_size() in kzalloc()
From
Date
On 1/4/19 10:22 PM, Gustavo A. R. Silva wrote:
> One of the more common cases of allocation size calculations is finding the
> size of a structure that has a zero-sized array at the end, along with memory
> for some number of elements for that array. For example:
>
> struct foo {
> int stuff;
> void *entry[];
> };
>
> instance = kzalloc(sizeof(struct foo) + sizeof(void *) * count, GFP_KERNEL);
>
> Instead of leaving these open-coded and prone to type mistakes, we can now
> use the new struct_size() helper:
>
> instance = kzalloc(struct_size(instance, entry, count), GFP_KERNEL);
>
> This code was detected with the help of Coccinelle.
>
> Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
> ---
> drivers/scsi/advansys.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/scsi/advansys.c b/drivers/scsi/advansys.c
> index d37584403c33..6c274e6e1c33 100644
> --- a/drivers/scsi/advansys.c
> +++ b/drivers/scsi/advansys.c
> @@ -7576,8 +7576,8 @@ static int asc_build_req(struct asc_board *boardp, struct scsi_cmnd *scp,
> return ASC_ERROR;
> }
>
> - asc_sg_head = kzalloc(sizeof(asc_scsi_q->sg_head) +
> - use_sg * sizeof(struct asc_sg_list), GFP_ATOMIC);
> + asc_sg_head = kzalloc(struct_size(asc_sg_head, sg_list, use_sg),
> + GFP_ATOMIC);
> if (!asc_sg_head) {
> scsi_dma_unmap(scp);
> scp->result = HOST_BYTE(DID_SOFT_ERROR);
>
If you want ...

Reviewed-by: Hannes Reinecke <hare@suse.com>

Cheers,

Hannes

\
 
 \ /
  Last update: 2019-01-11 16:47    [W:0.804 / U:0.724 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site