net: wwan: t7xx: Prefer struct_size over open coded arithmetic
PATCH 1/1 net: wwan: t7xx: Prefer struct_size over open coded arithmetic
This is an effort to get rid of all multiplications from allocation functions in order to prevent integer overflows [1][2].
As the "port_prox" variable is a pointer to "struct port_proxy" and this structure ends in a flexible array:
struct port_proxy {
[...]
struct t7xx_port ports[];
};the preferred way in the kernel is to use the struct_size() helper to do the arithmetic instead of the argument "size + size * count" in the devm_kzalloc() function.
This way, the code is more readable and safer.
--- drivers/net/wwan/t7xx/t7xx_port_proxy.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/net/wwan/t7xx/t7xx_port_proxy.c +4 -2
@@ -543,8 +543,10 @@ static int t7xx_proxy_alloc(struct t7xx_modem *md)struct device *dev = &md->t7xx_dev->pdev->dev;struct port_proxy *port_prox; - port_prox =devm_kzalloc (dev,sizeof (*port_prox) + -sizeof (struct t7xx_port ) *T7XX_MAX_POSSIBLE_PORTS_NUM , + port_prox =devm_kzalloc (dev, +struct_size (port_prox, + ports, +T7XX_MAX_POSSIBLE_PORTS_NUM ),GFP_KERNEL );if (!port_prox)return -ENOMEM ;
--
2.25.1
Quoted 15 lines · Erick Archer
This is an effort to get rid of all multiplications from allocation functions in order to prevent integer overflows [1][2].
As the "
port_prox" variable is a pointer to "structport_proxy" and this structure ends in a flexible array:struct port_proxy { [...] struct t7xx_port ports[]; };the preferred way in the kernel is to use the
struct_size()helper to do the arithmetic instead of the argument "size + size * count" in thedevm_kzalloc()function.
Quoted 6 lines
This way, the code is more readable and safer.
Signed-off-by Erick Archer <[email protected]>
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 6 lines · On Sat, 24 Feb 2024 19:19:32 +0100 you wrote:
This is an effort to get rid of all multiplications from allocation functions in order to prevent integer overflows [1][2].
As the "
port_prox" variable is a pointer to "structport_proxy" and this structure ends in a flexible array:
Quoted 6 lines
struct port_proxy { [...] struct t7xx_port ports[]; };[...]
Here is the summary with links:
- net: wwan: t7xx: Prefer struct_size over open coded arithmetic
https://git.kernel.org/netdev/net-next/c/848e34ca2030
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html