public inbox for linux-wireless@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] wifi: rtw89: debug: Fix memory leak in __print_txpwr_map()
@ 2026-01-16 13:08 Zilin Guan
  2026-01-20  3:27 ` Zong-Zhe Yang
  2026-01-22  2:02 ` Ping-Ke Shih
  0 siblings, 2 replies; 5+ messages in thread
From: Zilin Guan @ 2026-01-16 13:08 UTC (permalink / raw)
  To: pkshih; +Cc: kevin_yang, linux-wireless, linux-kernel, jianhao.xu, Zilin Guan

In __print_txpwr_map(), memory is allocated to bufp via vzalloc().
If max_valid_addr is 0, the function returns -EOPNOTSUPP immediately
without freeing bufp, leading to a memory leak.

Since the validation of max_valid_addr does not depend on the allocated
memory, fix this by moving the vzalloc() call after the check.

Compile tested only. Issue found using a prototype static analysis tool
and code review.

Fixes: 036042e15770 ("wifi: rtw89: debug: txpwr table supports Wi-Fi 7 chips")
Suggested-by: Zong-Zhe Yang <kevin_yang@realtek.com>
Signed-off-by: Zilin Guan <zilin@seu.edu.cn>
---
Changes in v2:
- Move memory allocation after validation check to avoid leak.

 drivers/net/wireless/realtek/rtw89/debug.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/net/wireless/realtek/rtw89/debug.c b/drivers/net/wireless/realtek/rtw89/debug.c
index 1264c2f82600..987eef8170f2 100644
--- a/drivers/net/wireless/realtek/rtw89/debug.c
+++ b/drivers/net/wireless/realtek/rtw89/debug.c
@@ -825,10 +825,6 @@ static ssize_t __print_txpwr_map(struct rtw89_dev *rtwdev, char *buf, size_t buf
 	s8 *bufp, tmp;
 	int ret;
 
-	bufp = vzalloc(map->addr_to - map->addr_from + 4);
-	if (!bufp)
-		return -ENOMEM;
-
 	if (path_num == 1)
 		max_valid_addr = map->addr_to_1ss;
 	else
@@ -837,6 +833,10 @@ static ssize_t __print_txpwr_map(struct rtw89_dev *rtwdev, char *buf, size_t buf
 	if (max_valid_addr == 0)
 		return -EOPNOTSUPP;
 
+	bufp = vzalloc(map->addr_to - map->addr_from + 4);
+	if (!bufp)
+		return -ENOMEM;
+
 	for (addr = map->addr_from; addr <= max_valid_addr; addr += 4) {
 		ret = rtw89_mac_txpwr_read32(rtwdev, RTW89_PHY_0, addr, &val);
 		if (ret)
-- 
2.34.1


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

* RE: [PATCH v2] wifi: rtw89: debug: Fix memory leak in __print_txpwr_map()
  2026-01-16 13:08 [PATCH v2] wifi: rtw89: debug: Fix memory leak in __print_txpwr_map() Zilin Guan
@ 2026-01-20  3:27 ` Zong-Zhe Yang
  2026-01-20  3:31   ` Ping-Ke Shih
  2026-01-22  2:02 ` Ping-Ke Shih
  1 sibling, 1 reply; 5+ messages in thread
From: Zong-Zhe Yang @ 2026-01-20  3:27 UTC (permalink / raw)
  To: Zilin Guan, Ping-Ke Shih
  Cc: linux-wireless@vger.kernel.org, linux-kernel@vger.kernel.org,
	jianhao.xu@seu.edu.cn

Zilin Guan <zilin@seu.edu.cn> wrote:
> 
> In __print_txpwr_map(), memory is allocated to bufp via vzalloc().
> If max_valid_addr is 0, the function returns -EOPNOTSUPP immediately
> without freeing bufp, leading to a memory leak.
> 
> Since the validation of max_valid_addr does not depend on the allocated
> memory, fix this by moving the vzalloc() call after the check.
> 
> Compile tested only. Issue found using a prototype static analysis tool
> and code review.
> 
> Fixes: 036042e15770 ("wifi: rtw89: debug: txpwr table supports Wi-Fi 7 chips")
> Suggested-by: Zong-Zhe Yang <kevin_yang@realtek.com>
> Signed-off-by: Zilin Guan <zilin@seu.edu.cn>
> ---
> Changes in v2:
> - Move memory allocation after validation check to avoid leak.
> 
>  drivers/net/wireless/realtek/rtw89/debug.c | 8 ++++----
>  1 file changed, 4 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/net/wireless/realtek/rtw89/debug.c
> b/drivers/net/wireless/realtek/rtw89/debug.c
> index 1264c2f82600..987eef8170f2 100644
> --- a/drivers/net/wireless/realtek/rtw89/debug.c
> +++ b/drivers/net/wireless/realtek/rtw89/debug.c
> @@ -825,10 +825,6 @@ static ssize_t __print_txpwr_map(struct rtw89_dev *rtwdev, char
> *buf, size_t buf
>         s8 *bufp, tmp;
>         int ret;
> 
> -       bufp = vzalloc(map->addr_to - map->addr_from + 4);
> -       if (!bufp)
> -               return -ENOMEM;
> -
>         if (path_num == 1)
>                 max_valid_addr = map->addr_to_1ss;
>         else
> @@ -837,6 +833,10 @@ static ssize_t __print_txpwr_map(struct rtw89_dev *rtwdev, char
> *buf, size_t buf
>         if (max_valid_addr == 0)
>                 return -EOPNOTSUPP;
> 
> +       bufp = vzalloc(map->addr_to - map->addr_from + 4);
> +       if (!bufp)
> +               return -ENOMEM;
> +
>         for (addr = map->addr_from; addr <= max_valid_addr; addr += 4) {
>                 ret = rtw89_mac_txpwr_read32(rtwdev, RTW89_PHY_0, addr, &val);
>                 if (ret)
> --
> 2.34.1

Looks good to me.
Thanks.

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

* RE: [PATCH v2] wifi: rtw89: debug: Fix memory leak in __print_txpwr_map()
  2026-01-20  3:27 ` Zong-Zhe Yang
@ 2026-01-20  3:31   ` Ping-Ke Shih
  2026-01-20  3:39     ` Zong-Zhe Yang
  0 siblings, 1 reply; 5+ messages in thread
From: Ping-Ke Shih @ 2026-01-20  3:31 UTC (permalink / raw)
  To: Zong-Zhe Yang, Zilin Guan
  Cc: linux-wireless@vger.kernel.org, linux-kernel@vger.kernel.org,
	jianhao.xu@seu.edu.cn


Zong-Zhe Yang <kevin_yang@realtek.com> wrote:
> Zilin Guan <zilin@seu.edu.cn> wrote:
> >
> > In __print_txpwr_map(), memory is allocated to bufp via vzalloc().
> > If max_valid_addr is 0, the function returns -EOPNOTSUPP immediately
> > without freeing bufp, leading to a memory leak.
> >
> > Since the validation of max_valid_addr does not depend on the allocated
> > memory, fix this by moving the vzalloc() call after the check.
> >
> > Compile tested only. Issue found using a prototype static analysis tool
> > and code review.
> >
> > Fixes: 036042e15770 ("wifi: rtw89: debug: txpwr table supports Wi-Fi 7 chips")
> > Suggested-by: Zong-Zhe Yang <kevin_yang@realtek.com>
> > Signed-off-by: Zilin Guan <zilin@seu.edu.cn>
> > ---
> > Changes in v2:
> > - Move memory allocation after validation check to avoid leak.
> >
> >  drivers/net/wireless/realtek/rtw89/debug.c | 8 ++++----
> >  1 file changed, 4 insertions(+), 4 deletions(-)
> >
> > diff --git a/drivers/net/wireless/realtek/rtw89/debug.c
> > b/drivers/net/wireless/realtek/rtw89/debug.c
> > index 1264c2f82600..987eef8170f2 100644
> > --- a/drivers/net/wireless/realtek/rtw89/debug.c
> > +++ b/drivers/net/wireless/realtek/rtw89/debug.c
> > @@ -825,10 +825,6 @@ static ssize_t __print_txpwr_map(struct rtw89_dev *rtwdev, char
> > *buf, size_t buf
> >         s8 *bufp, tmp;
> >         int ret;
> >
> > -       bufp = vzalloc(map->addr_to - map->addr_from + 4);
> > -       if (!bufp)
> > -               return -ENOMEM;
> > -
> >         if (path_num == 1)
> >                 max_valid_addr = map->addr_to_1ss;
> >         else
> > @@ -837,6 +833,10 @@ static ssize_t __print_txpwr_map(struct rtw89_dev *rtwdev, char
> > *buf, size_t buf
> >         if (max_valid_addr == 0)
> >                 return -EOPNOTSUPP;
> >
> > +       bufp = vzalloc(map->addr_to - map->addr_from + 4);
> > +       if (!bufp)
> > +               return -ENOMEM;
> > +
> >         for (addr = map->addr_from; addr <= max_valid_addr; addr += 4) {
> >                 ret = rtw89_mac_txpwr_read32(rtwdev, RTW89_PHY_0, addr, &val);
> >                 if (ret)
> > --
> > 2.34.1
> 
> Looks good to me.

I suppose I can add 
Reviewed-by: Zong-Zhe Yang <kevin_yang@realtek.com>

Okay?


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

* RE: [PATCH v2] wifi: rtw89: debug: Fix memory leak in __print_txpwr_map()
  2026-01-20  3:31   ` Ping-Ke Shih
@ 2026-01-20  3:39     ` Zong-Zhe Yang
  0 siblings, 0 replies; 5+ messages in thread
From: Zong-Zhe Yang @ 2026-01-20  3:39 UTC (permalink / raw)
  To: Ping-Ke Shih, Zilin Guan
  Cc: linux-wireless@vger.kernel.org, linux-kernel@vger.kernel.org,
	jianhao.xu@seu.edu.cn

Ping-Ke Shih <pkshih@realtek.com> wrote:
> Zong-Zhe Yang <kevin_yang@realtek.com> wrote:
> > Zilin Guan <zilin@seu.edu.cn> wrote:
> > >
> > > In __print_txpwr_map(), memory is allocated to bufp via vzalloc().
> > > If max_valid_addr is 0, the function returns -EOPNOTSUPP immediately
> > > without freeing bufp, leading to a memory leak.
> > >
> > > Since the validation of max_valid_addr does not depend on the allocated
> > > memory, fix this by moving the vzalloc() call after the check.
> > >
> > > Compile tested only. Issue found using a prototype static analysis tool
> > > and code review.
> > >
> > > Fixes: 036042e15770 ("wifi: rtw89: debug: txpwr table supports Wi-Fi 7 chips")
> > > Suggested-by: Zong-Zhe Yang <kevin_yang@realtek.com>
> > > Signed-off-by: Zilin Guan <zilin@seu.edu.cn>
> > > ---
> > > Changes in v2:
> > > - Move memory allocation after validation check to avoid leak.
> > >
> > >  drivers/net/wireless/realtek/rtw89/debug.c | 8 ++++----
> > >  1 file changed, 4 insertions(+), 4 deletions(-)
> > >
> > > diff --git a/drivers/net/wireless/realtek/rtw89/debug.c
> > > b/drivers/net/wireless/realtek/rtw89/debug.c
> > > index 1264c2f82600..987eef8170f2 100644
> > > --- a/drivers/net/wireless/realtek/rtw89/debug.c
> > > +++ b/drivers/net/wireless/realtek/rtw89/debug.c
> > > @@ -825,10 +825,6 @@ static ssize_t __print_txpwr_map(struct rtw89_dev *rtwdev,
> char
> > > *buf, size_t buf
> > >         s8 *bufp, tmp;
> > >         int ret;
> > >
> > > -       bufp = vzalloc(map->addr_to - map->addr_from + 4);
> > > -       if (!bufp)
> > > -               return -ENOMEM;
> > > -
> > >         if (path_num == 1)
> > >                 max_valid_addr = map->addr_to_1ss;
> > >         else
> > > @@ -837,6 +833,10 @@ static ssize_t __print_txpwr_map(struct rtw89_dev *rtwdev,
> char
> > > *buf, size_t buf
> > >         if (max_valid_addr == 0)
> > >                 return -EOPNOTSUPP;
> > >
> > > +       bufp = vzalloc(map->addr_to - map->addr_from + 4);
> > > +       if (!bufp)
> > > +               return -ENOMEM;
> > > +
> > >         for (addr = map->addr_from; addr <= max_valid_addr; addr += 4) {
> > >                 ret = rtw89_mac_txpwr_read32(rtwdev, RTW89_PHY_0, addr, &val);
> > >                 if (ret)
> > > --
> > > 2.34.1
> >
> > Looks good to me.
> 
> I suppose I can add
> Reviewed-by: Zong-Zhe Yang <kevin_yang@realtek.com>
> 
> Okay?

Okay, thanks.

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

* Re: [PATCH v2] wifi: rtw89: debug: Fix memory leak in __print_txpwr_map()
  2026-01-16 13:08 [PATCH v2] wifi: rtw89: debug: Fix memory leak in __print_txpwr_map() Zilin Guan
  2026-01-20  3:27 ` Zong-Zhe Yang
@ 2026-01-22  2:02 ` Ping-Ke Shih
  1 sibling, 0 replies; 5+ messages in thread
From: Ping-Ke Shih @ 2026-01-22  2:02 UTC (permalink / raw)
  To: Zilin Guan, pkshih
  Cc: kevin_yang, linux-wireless, linux-kernel, jianhao.xu, Zilin Guan

Zilin Guan <zilin@seu.edu.cn> wrote:

> In __print_txpwr_map(), memory is allocated to bufp via vzalloc().
> If max_valid_addr is 0, the function returns -EOPNOTSUPP immediately
> without freeing bufp, leading to a memory leak.
> 
> Since the validation of max_valid_addr does not depend on the allocated
> memory, fix this by moving the vzalloc() call after the check.
> 
> Compile tested only. Issue found using a prototype static analysis tool
> and code review.
> 
> Fixes: 036042e15770 ("wifi: rtw89: debug: txpwr table supports Wi-Fi 7 chips")
> Suggested-by: Zong-Zhe Yang <kevin_yang@realtek.com>
> Signed-off-by: Zilin Guan <zilin@seu.edu.cn>
> Reviewed-by: Zong-Zhe Yang <kevin_yang@realtek.com>

1 patch(es) applied to rtw-next branch of rtw.git, thanks.

6070a44051b1 wifi: rtw89: debug: Fix memory leak in __print_txpwr_map()

---
https://github.com/pkshih/rtw.git


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

end of thread, other threads:[~2026-01-22  2:02 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-16 13:08 [PATCH v2] wifi: rtw89: debug: Fix memory leak in __print_txpwr_map() Zilin Guan
2026-01-20  3:27 ` Zong-Zhe Yang
2026-01-20  3:31   ` Ping-Ke Shih
2026-01-20  3:39     ` Zong-Zhe Yang
2026-01-22  2:02 ` Ping-Ke Shih

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox