* [PATCH 1/3] cw1200: Don't leak memory if krealloc failes
@ 2016-09-30 12:11 Johannes Thumshirn
[not found] ` <1475237495-15030-1-git-send-email-jthumshirn-l3A5Bk7waGM@public.gmane.org>
2016-09-30 12:56 ` Sergei Shtylyov
0 siblings, 2 replies; 5+ messages in thread
From: Johannes Thumshirn @ 2016-09-30 12:11 UTC (permalink / raw)
To: Solomon Peachy, Kalle Valo
Cc: linux-wireless, netdev, linux-kernel, Johannes Thumshirn
The call to krealloc() in wsm_buf_reserve() directly assigns the newly
returned memory to buf->begin. This is all fine except when krealloc()
failes we loose the ability to free the old memory pointed to by
buf->begin. If we just create a temporary variable to assign memory to
and assign the memory to it we can mitigate the memory leak.
Signed-off-by: Johannes Thumshirn <jthumshirn@suse.de>
---
drivers/net/wireless/st/cw1200/wsm.c | 16 +++++++++-------
1 file changed, 9 insertions(+), 7 deletions(-)
diff --git a/drivers/net/wireless/st/cw1200/wsm.c b/drivers/net/wireless/st/cw1200/wsm.c
index 680d60e..12fad99 100644
--- a/drivers/net/wireless/st/cw1200/wsm.c
+++ b/drivers/net/wireless/st/cw1200/wsm.c
@@ -1807,16 +1807,18 @@ static int wsm_buf_reserve(struct wsm_buf *buf, size_t extra_size)
{
size_t pos = buf->data - buf->begin;
size_t size = pos + extra_size;
+ u8 *tmp;
size = round_up(size, FWLOAD_BLOCK_SIZE);
- buf->begin = krealloc(buf->begin, size, GFP_KERNEL | GFP_DMA);
- if (buf->begin) {
- buf->data = &buf->begin[pos];
- buf->end = &buf->begin[size];
- return 0;
- } else {
- buf->end = buf->data = buf->begin;
+ tmp = krealloc(buf->begin, size, GFP_KERNEL | GFP_DMA);
+ if (tmp) {
+ wsm_buf_deinit(buf);
return -ENOMEM;
}
+
+ buf->begin = tmp;
+ buf->data = &buf->begin[pos];
+ buf->end = &buf->begin[size];
+ return 0;
}
--
1.8.5.6
^ permalink raw reply related [flat|nested] 5+ messages in thread[parent not found: <1475237495-15030-1-git-send-email-jthumshirn-l3A5Bk7waGM@public.gmane.org>]
* Re: [PATCH 1/3] cw1200: Don't leak memory if krealloc failes
[not found] ` <1475237495-15030-1-git-send-email-jthumshirn-l3A5Bk7waGM@public.gmane.org>
@ 2016-09-30 12:29 ` Johannes Berg
2016-09-30 12:33 ` Johannes Thumshirn
0 siblings, 1 reply; 5+ messages in thread
From: Johannes Berg @ 2016-09-30 12:29 UTC (permalink / raw)
To: Johannes Thumshirn, Solomon Peachy, Kalle Valo
Cc: linux-wireless-u79uwXL29TY76Z2rM5mHXA,
netdev-u79uwXL29TY76Z2rM5mHXA,
linux-kernel-u79uwXL29TY76Z2rM5mHXA
> + tmp = krealloc(buf->begin, size, GFP_KERNEL | GFP_DMA);
> + if (tmp) {
>
I think that check is inverted?
johannes
^ permalink raw reply [flat|nested] 5+ messages in thread* Re: [PATCH 1/3] cw1200: Don't leak memory if krealloc failes
2016-09-30 12:29 ` Johannes Berg
@ 2016-09-30 12:33 ` Johannes Thumshirn
0 siblings, 0 replies; 5+ messages in thread
From: Johannes Thumshirn @ 2016-09-30 12:33 UTC (permalink / raw)
To: Johannes Berg
Cc: Solomon Peachy, Kalle Valo, linux-wireless, netdev, linux-kernel
On Fri, Sep 30, 2016 at 02:29:39PM +0200, Johannes Berg wrote:
>
> > + tmp = krealloc(buf->begin, size, GFP_KERNEL | GFP_DMA);
> > + if (tmp) {
> >
> I think that check is inverted?
>
> johannes
Fu.. you're right, of cause it's !tmp.
I'll resend.
Thanks,
Johannes
--
Johannes Thumshirn Storage
jthumshirn@suse.de +49 911 74053 689
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg
GF: Felix Imendörffer, Jane Smithard, Graham Norton
HRB 21284 (AG Nürnberg)
Key fingerprint = EC38 9CAB C2C4 F25D 8600 D0D0 0393 969D 2D76 0850
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH 1/3] cw1200: Don't leak memory if krealloc failes
2016-09-30 12:11 [PATCH 1/3] cw1200: Don't leak memory if krealloc failes Johannes Thumshirn
[not found] ` <1475237495-15030-1-git-send-email-jthumshirn-l3A5Bk7waGM@public.gmane.org>
@ 2016-09-30 12:56 ` Sergei Shtylyov
[not found] ` <25a38254-f0e3-1f4e-de46-688d9fd1b736-M4DtvfQ/ZS1MRgGoP+s0PdBPR1lH4CV8@public.gmane.org>
1 sibling, 1 reply; 5+ messages in thread
From: Sergei Shtylyov @ 2016-09-30 12:56 UTC (permalink / raw)
To: Johannes Thumshirn, Solomon Peachy, Kalle Valo
Cc: linux-wireless, netdev, linux-kernel
Hello.
On 9/30/2016 3:11 PM, Johannes Thumshirn wrote:
> The call to krealloc() in wsm_buf_reserve() directly assigns the newly
> returned memory to buf->begin. This is all fine except when krealloc()
> failes we loose the ability to free the old memory pointed to by
Fails.
> buf->begin. If we just create a temporary variable to assign memory to
> and assign the memory to it we can mitigate the memory leak.
>
> Signed-off-by: Johannes Thumshirn <jthumshirn@suse.de>
> ---
> drivers/net/wireless/st/cw1200/wsm.c | 16 +++++++++-------
> 1 file changed, 9 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/net/wireless/st/cw1200/wsm.c b/drivers/net/wireless/st/cw1200/wsm.c
> index 680d60e..12fad99 100644
> --- a/drivers/net/wireless/st/cw1200/wsm.c
> +++ b/drivers/net/wireless/st/cw1200/wsm.c
> @@ -1807,16 +1807,18 @@ static int wsm_buf_reserve(struct wsm_buf *buf, size_t extra_size)
> {
> size_t pos = buf->data - buf->begin;
> size_t size = pos + extra_size;
> + u8 *tmp;
>
> size = round_up(size, FWLOAD_BLOCK_SIZE);
>
> - buf->begin = krealloc(buf->begin, size, GFP_KERNEL | GFP_DMA);
> - if (buf->begin) {
> - buf->data = &buf->begin[pos];
> - buf->end = &buf->begin[size];
> - return 0;
> - } else {
> - buf->end = buf->data = buf->begin;
> + tmp = krealloc(buf->begin, size, GFP_KERNEL | GFP_DMA);
> + if (tmp) {
!tmp, you mean?
> + wsm_buf_deinit(buf);
> return -ENOMEM;
> }
> +
> + buf->begin = tmp;
> + buf->data = &buf->begin[pos];
> + buf->end = &buf->begin[size];
> + return 0;
> }
MBR, Sergei
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2016-09-30 13:00 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-09-30 12:11 [PATCH 1/3] cw1200: Don't leak memory if krealloc failes Johannes Thumshirn
[not found] ` <1475237495-15030-1-git-send-email-jthumshirn-l3A5Bk7waGM@public.gmane.org>
2016-09-30 12:29 ` Johannes Berg
2016-09-30 12:33 ` Johannes Thumshirn
2016-09-30 12:56 ` Sergei Shtylyov
[not found] ` <25a38254-f0e3-1f4e-de46-688d9fd1b736-M4DtvfQ/ZS1MRgGoP+s0PdBPR1lH4CV8@public.gmane.org>
2016-09-30 13:00 ` Johannes Thumshirn
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).