diff mbox

[net-next] tipc: fix a possible memory leak

Message ID 1398393855-24772-1-git-send-email-ying.xue@windriver.com
State Accepted, archived
Delegated to: David Miller
Headers show

Commit Message

Ying Xue April 25, 2014, 2:44 a.m. UTC
The commit a8b9b96e959f3c035af20b1bd2ba67b0b7269b19 ("tipc: fix race
in disc create/delete") leads to the following static checker warning:

	net/tipc/discover.c:352 tipc_disc_create()
		warn: possible memory leak of 'req'

The risk of memory leak really exists in practice. Especially when
it's failed to allocate memory for "req->buf", tipc_disc_create()
doesn't free its allocated memory, instead just directly returns
with ENOMEM error code. In this situation, memory leak, of course,
happens.

Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
Signed-off-by: Ying Xue <ying.xue@windriver.com>
---
 net/tipc/discover.c |    4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Comments

David Miller April 27, 2014, 11:08 p.m. UTC | #1
From: Ying Xue <ying.xue@windriver.com>
Date: Fri, 25 Apr 2014 10:44:15 +0800

> The commit a8b9b96e959f3c035af20b1bd2ba67b0b7269b19 ("tipc: fix race
> in disc create/delete") leads to the following static checker warning:
> 
> 	net/tipc/discover.c:352 tipc_disc_create()
> 		warn: possible memory leak of 'req'
> 
> The risk of memory leak really exists in practice. Especially when
> it's failed to allocate memory for "req->buf", tipc_disc_create()
> doesn't free its allocated memory, instead just directly returns
> with ENOMEM error code. In this situation, memory leak, of course,
> happens.
> 
> Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
> Signed-off-by: Ying Xue <ying.xue@windriver.com>

Applied, thanks.
--
To unsubscribe from this list: send the line "unsubscribe netdev" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/net/tipc/discover.c b/net/tipc/discover.c
index ada42e4..bd35c4a 100644
--- a/net/tipc/discover.c
+++ b/net/tipc/discover.c
@@ -348,8 +348,10 @@  int tipc_disc_create(struct tipc_bearer *b_ptr, struct tipc_media_addr *dest)
 		return -ENOMEM;
 
 	req->buf = tipc_buf_acquire(INT_H_SIZE);
-	if (!req->buf)
+	if (!req->buf) {
+		kfree(req);
 		return -ENOMEM;
+	}
 
 	tipc_disc_init_msg(req->buf, DSC_REQ_MSG, b_ptr);
 	memcpy(&req->dest, dest, sizeof(*dest));