diff mbox

Fix memory leak if nla_put fails

Message ID 1448365527-14498-1-git-send-email-rahul.jain@samsung.com (mailing list archive)
State Changes Requested
Delegated to: Johannes Berg
Headers show

Commit Message

Rahul Jain Nov. 24, 2015, 11:45 a.m. UTC
From: Amit Khatri <amit.khatri@samsung.com>

Signed-off-by: Amit Khatri <amit.khatri@samsung.com>
Signed-off-by: Rahul Jain  <rahul.jain@samsung.com>

avoid memory leak because of nla_put_failure
---
 coalesce.c |  7 ++++++-
 wowlan.c   | 28 ++++++++++++++++++++++------
 2 files changed, 28 insertions(+), 7 deletions(-)

Comments

Johannes Berg Nov. 26, 2015, 5:40 p.m. UTC | #1
On Tue, 2015-11-24 at 17:15 +0530, Rahul Jain wrote:
> From: Amit Khatri <amit.khatri@samsung.com>
> 
> Signed-off-by: Amit Khatri <amit.khatri@samsung.com>
> Signed-off-by: Rahul Jain  <rahul.jain@samsung.com>
> 
> avoid memory leak because of nla_put_failure
> 
Please fix coding style, and put the message before signed-off-by :)

johannes
--
To unsubscribe from this list: send the line "unsubscribe linux-wireless" 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/coalesce.c b/coalesce.c
index 36dcaef..9b13ab3 100644
--- a/coalesce.c
+++ b/coalesce.c
@@ -124,7 +124,8 @@  static int handle_coalesce_enable(struct nl80211_state *state,
 					nla_nest_end(msg, nl_pat);
 					free(mask);
 					free(pat);
-
+					pat=NULL;
+					mask=NULL;
 					if (!next_pat)
 						break;
 					cur_pat = next_pat;
@@ -155,6 +156,10 @@  static int handle_coalesce_enable(struct nl80211_state *state,
 		err = 1;
 	goto close;
 nla_put_failure:
+	if(pat)
+		free(pat);
+	if(mask)
+		free(mask);
 	err = -ENOBUFS;
 close:
 	fclose(f);
diff --git a/wowlan.c b/wowlan.c
index c30eab7..2e1d43d 100644
--- a/wowlan.c
+++ b/wowlan.c
@@ -89,7 +89,10 @@  static int wowlan_parse_tcp_file(struct nl_msg *msg, const char *fn)
 
 			if (!pkt)
 				goto close;
-			NLA_PUT(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD, len, pkt);
+			if(nla_put(msg, NL80211_WOWLAN_TCP_DATA_PAYLOAD, len, pkt) < 0){
+				free(pkt);
+				goto nla_put_failure;
+			}
 			free(pkt);
 		} else if (strncmp(buf, "data.interval=", 14) == 0) {
 			NLA_PUT_U32(msg, NL80211_WOWLAN_TCP_DATA_INTERVAL,
@@ -97,13 +100,20 @@  static int wowlan_parse_tcp_file(struct nl_msg *msg, const char *fn)
 		} else if (strncmp(buf, "wake=", 5) == 0) {
 			unsigned char *pat, *mask;
 			size_t patlen;
-
 			if (parse_hex_mask(buf + 5, &pat, &patlen, &mask))
 				goto close;
-			NLA_PUT(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
-				DIV_ROUND_UP(patlen, 8), mask);
-			NLA_PUT(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
-				patlen, pat);
+			if(nla_put(msg, NL80211_WOWLAN_TCP_WAKE_MASK,
+				DIV_ROUND_UP(patlen, 8), mask) < 0){
+				free(mask);
+				free(pat);
+				goto nla_put_failure;
+			}
+			if(nla_put(msg, NL80211_WOWLAN_TCP_WAKE_PAYLOAD,
+				patlen, pat) < 0){
+				free(pat);
+				free(mask);
+				goto nla_put_failure;
+			}
 			free(mask);
 			free(pat);
 		} else if (strncmp(buf, "data.seq=", 9) == 0) {
@@ -299,6 +309,8 @@  static int handle_wowlan_enable(struct nl80211_state *state,
 			nla_nest_end(patterns, pattern);
 			free(mask);
 			free(pat);
+			pat=NULL;
+			mask=NULL;
 			break;
 		}
 		argv++;
@@ -312,6 +324,10 @@  static int handle_wowlan_enable(struct nl80211_state *state,
 	nla_nest_end(msg, wowlan);
 	err = 0;
  nla_put_failure:
+	if(pat)
+		free(pat);
+	if(mask)
+		free(mask);
 	nlmsg_free(patterns);
 	return err;
 }