linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] ath: dfs_pattern_detector: Avoid open coded arithmetic in memory allocation
@ 2021-12-27 16:58 Christophe JAILLET
  2022-01-17 12:41 ` Kalle Valo
  0 siblings, 1 reply; 2+ messages in thread
From: Christophe JAILLET @ 2021-12-27 16:58 UTC (permalink / raw)
  To: kvalo, davem, kuba
  Cc: linux-wireless, netdev, linux-kernel, kernel-janitors,
	Christophe JAILLET

kmalloc_array()/kcalloc() should be used to avoid potential overflow when
a multiplication is needed to compute the size of the requested memory.

kmalloc_array() can be used here instead of kcalloc() because the array is
fully initialized in the next 'for' loop.

Finally, 'cd->detectors' is defined as 'struct pri_detector **detectors;'.
So 'cd->detectors' and '*cd->detectors' are both some pointer.
So use a more logical 'sizeof(*cd->detectors)'.

Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
 drivers/net/wireless/ath/dfs_pattern_detector.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/drivers/net/wireless/ath/dfs_pattern_detector.c b/drivers/net/wireless/ath/dfs_pattern_detector.c
index 75cb53a3ec15..27f4d74a41c8 100644
--- a/drivers/net/wireless/ath/dfs_pattern_detector.c
+++ b/drivers/net/wireless/ath/dfs_pattern_detector.c
@@ -197,7 +197,7 @@ static void channel_detector_exit(struct dfs_pattern_detector *dpd,
 static struct channel_detector *
 channel_detector_create(struct dfs_pattern_detector *dpd, u16 freq)
 {
-	u32 sz, i;
+	u32 i;
 	struct channel_detector *cd;
 
 	cd = kmalloc(sizeof(*cd), GFP_ATOMIC);
@@ -206,8 +206,8 @@ channel_detector_create(struct dfs_pattern_detector *dpd, u16 freq)
 
 	INIT_LIST_HEAD(&cd->head);
 	cd->freq = freq;
-	sz = sizeof(cd->detectors) * dpd->num_radar_types;
-	cd->detectors = kzalloc(sz, GFP_ATOMIC);
+	cd->detectors = kmalloc_array(dpd->num_radar_types,
+				      sizeof(*cd->detectors), GFP_ATOMIC);
 	if (cd->detectors == NULL)
 		goto fail;
 
-- 
2.32.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] ath: dfs_pattern_detector: Avoid open coded arithmetic in memory allocation
  2021-12-27 16:58 [PATCH] ath: dfs_pattern_detector: Avoid open coded arithmetic in memory allocation Christophe JAILLET
@ 2022-01-17 12:41 ` Kalle Valo
  0 siblings, 0 replies; 2+ messages in thread
From: Kalle Valo @ 2022-01-17 12:41 UTC (permalink / raw)
  To: Christophe JAILLET
  Cc: davem, kuba, linux-wireless, netdev, linux-kernel,
	kernel-janitors, Christophe JAILLET

Christophe JAILLET <christophe.jaillet@wanadoo.fr> wrote:

> kmalloc_array()/kcalloc() should be used to avoid potential overflow when
> a multiplication is needed to compute the size of the requested memory.
> 
> kmalloc_array() can be used here instead of kcalloc() because the array is
> fully initialized in the next 'for' loop.
> 
> Finally, 'cd->detectors' is defined as 'struct pri_detector **detectors;'.
> So 'cd->detectors' and '*cd->detectors' are both some pointer.
> So use a more logical 'sizeof(*cd->detectors)'.
> 
> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
> Signed-off-by: Kalle Valo <quic_kvalo@quicinc.com>

Patch applied to ath-next branch of ath.git, thanks.

a063b650ce5d ath: dfs_pattern_detector: Avoid open coded arithmetic in memory allocation

-- 
https://patchwork.kernel.org/project/linux-wireless/patch/0fbcd32a0384ac1f87c5a3549e505e4becc60226.1640624216.git.christophe.jaillet@wanadoo.fr/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-01-17 12:42 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-27 16:58 [PATCH] ath: dfs_pattern_detector: Avoid open coded arithmetic in memory allocation Christophe JAILLET
2022-01-17 12:41 ` Kalle Valo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).