Skip to content

Commit 330d558

Browse files
GustavoARSilvatorvalds
authored andcommitted
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: KSPP/linux#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>
1 parent 216ec27 commit 330d558

1 file changed

Lines changed: 3 additions & 5 deletions

File tree

‎drivers/rapidio/rio-scan.c‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -330,18 +330,16 @@ static struct rio_dev *rio_setup_device(struct rio_net *net,
330330
size_t size;
331331
u32 swpinfo = 0;
332332

333-
size = sizeof(struct rio_dev);
333+
size = sizeof(*rdev);
334334
if (rio_mport_read_config_32(port, destid, hopcount,
335335
RIO_PEF_CAR, &result))
336336
return NULL;
337337

338338
if (result & (RIO_PEF_SWITCH | RIO_PEF_MULTIPORT)) {
339339
rio_mport_read_config_32(port, destid, hopcount,
340340
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));
345343
}
346344

347345
rdev = kzalloc(size, GFP_KERNEL);

0 commit comments

Comments
 (0)