diff mbox series

net: wwan: t7xx: Prefer struct_size over open coded arithmetic

Message ID 20240224181932.2720-1-erick.archer@gmx.com (mailing list archive)
State Accepted
Commit 848e34ca203046c9b967034596828472f08e4ac7
Delegated to: Netdev Maintainers
Headers show
Series net: wwan: t7xx: Prefer struct_size over open coded arithmetic | expand

Checks

Context Check Description
netdev/series_format warning Single patches do not need cover letters; Target tree name not specified in the subject
netdev/tree_selection success Guessed tree name to be net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 940 this patch: 940
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers warning 4 maintainers not CCed: angelogioacchino.delregno@collabora.com linux-mediatek@lists.infradead.org linux-arm-kernel@lists.infradead.org matthias.bgg@gmail.com
netdev/build_clang success Errors and warnings before: 957 this patch: 957
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 957 this patch: 957
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 12 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
netdev/contest success net-next-2024-02-25--03-00 (tests: 1457)

Commit Message

Erick Archer Feb. 24, 2024, 6:19 p.m. UTC
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.

Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments [1]
Link: https://github.com/KSPP/linux/issues/160 [2]

Signed-off-by: Erick Archer <erick.archer@gmx.com>
---
 drivers/net/wwan/t7xx/t7xx_port_proxy.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

--
2.25.1

Comments

Sergey Ryazanov Feb. 24, 2024, 9:39 p.m. UTC | #1
On 24.02.2024 20:19, Erick Archer 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 "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.
> 
> Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments [1]
> Link: https://github.com/KSPP/linux/issues/160 [2]
> 
> Signed-off-by: Erick Archer <erick.archer@gmx.com>

Reviewed-by: Sergey Ryazanov <ryazanov.s.a@gmail.com>
patchwork-bot+netdevbpf@kernel.org Feb. 28, 2024, 2:20 a.m. UTC | #2
Hello:

This patch was applied to netdev/net-next.git (main)
by Jakub Kicinski <kuba@kernel.org>:

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 "struct port_proxy" and
> this structure ends in a flexible array:
> 
> 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!
diff mbox series

Patch

diff --git a/drivers/net/wwan/t7xx/t7xx_port_proxy.c b/drivers/net/wwan/t7xx/t7xx_port_proxy.c
index 8f5e01705af2..7d6388bf1d7c 100644
--- a/drivers/net/wwan/t7xx/t7xx_port_proxy.c
+++ b/drivers/net/wwan/t7xx/t7xx_port_proxy.c
@@ -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;