Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

drivers/rapidio/rio-scan.c: use struct_size() helper

Make use of the struct_size() helper instead of an open-coded version in
order to avoid any potential type mistakes.

Also, while there, use the preferred form for passing a size of a struct.
The alternative form where struct name is spelled out hurts readability
and introduces an opportunity for a bug when the pointer variable type is
changed but the corresponding sizeof that is passed as argument is not.

This issue was found with the help of Coccinelle and, audited and fixed
manually.

Addresses KSPP ID: https://github.com/KSPP/linux/issues/83

Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Cc: Matt Porter <mporter@kernel.crashing.org>
Cc: Alexandre Bounine <alex.bou9@gmail.com>
Link: http://lkml.kernel.org/r/20200619170445.GA22641@embeddedor
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Gustavo A. R. Silva and committed by
Linus Torvalds
330d5589 216ec27f

+3 -5
+3 -5
drivers/rapidio/rio-scan.c
··· 330 330 size_t size; 331 331 u32 swpinfo = 0; 332 332 333 - size = sizeof(struct rio_dev); 333 + size = sizeof(*rdev); 334 334 if (rio_mport_read_config_32(port, destid, hopcount, 335 335 RIO_PEF_CAR, &result)) 336 336 return NULL; ··· 338 338 if (result & (RIO_PEF_SWITCH | RIO_PEF_MULTIPORT)) { 339 339 rio_mport_read_config_32(port, destid, hopcount, 340 340 RIO_SWP_INFO_CAR, &swpinfo); 341 - if (result & RIO_PEF_SWITCH) { 342 - size += (RIO_GET_TOTAL_PORTS(swpinfo) * 343 - sizeof(rswitch->nextdev[0])) + sizeof(*rswitch); 344 - } 341 + if (result & RIO_PEF_SWITCH) 342 + size += struct_size(rswitch, nextdev, RIO_GET_TOTAL_PORTS(swpinfo)); 345 343 } 346 344 347 345 rdev = kzalloc(size, GFP_KERNEL);