public inbox for b.a.t.m.a.n@lists.open-mesh.org
 help / color / mirror / Atom feed
* [B.A.T.M.A.N.] [PATCH 01/10] batadv-vis: Avoid memory leak after failed realloc
@ 2014-05-24 11:44 Sven Eckelmann
  2014-05-24 11:44 ` [B.A.T.M.A.N.] [PATCH 02/10] alfred: Force null termination of string after strncpy Sven Eckelmann
                   ` (9 more replies)
  0 siblings, 10 replies; 12+ messages in thread
From: Sven Eckelmann @ 2014-05-24 11:44 UTC (permalink / raw)
  To: b.a.t.m.a.n; +Cc: Sven Eckelmann

realloc doesn't free the original buffer when the reallocation failed. An abort
of read_file without free'ing the buffer would leak it.

Signed-off-by: Sven Eckelmann <sven@narfation.org>

---
 vis/vis.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/vis/vis.c b/vis/vis.c
index 7a8e780..b51fede 100644
--- a/vis/vis.c
+++ b/vis/vis.c
@@ -40,7 +40,7 @@
 static char *read_file(char *fname)
 {
 	FILE *fp;
-	char *buf = NULL;
+	char *buf = NULL, *buf_tmp;
 	size_t size, ret;
 
 	fp = fopen(fname, "r");
@@ -51,10 +51,13 @@ static char *read_file(char *fname)
 	size = 0;
 	while (!feof(fp)) {
 
-		buf = realloc(buf, size + 4097);
-		if (!buf)
+		buf_tmp = realloc(buf, size + 4097);
+		if (!buf_tmp) {
+			free(buf);
 			return NULL;
+		}
 
+		buf = buf_tmp;
 		ret = fread(buf + size, 1, 4096, fp);
 		size += ret;
 	}
-- 
2.0.0.rc2


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

end of thread, other threads:[~2014-05-27 14:08 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-05-24 11:44 [B.A.T.M.A.N.] [PATCH 01/10] batadv-vis: Avoid memory leak after failed realloc Sven Eckelmann
2014-05-24 11:44 ` [B.A.T.M.A.N.] [PATCH 02/10] alfred: Force null termination of string after strncpy Sven Eckelmann
2014-05-24 11:44 ` [B.A.T.M.A.N.] [PATCH 03/10] batadv-vis: Avoid memory leak when tl parsing failed Sven Eckelmann
2014-05-24 11:44 ` [B.A.T.M.A.N.] [PATCH 04/10] alfred: Use pre-allocated object for global variables Sven Eckelmann
2014-05-24 11:44 ` [B.A.T.M.A.N.] [PATCH 05/10] batadv-vis: Avoid invalid access in orig_list Sven Eckelmann
2014-05-24 11:44 ` [B.A.T.M.A.N.] [PATCH 06/10] alfred-gpsd: Fix altitute verification check Sven Eckelmann
2014-05-24 14:45   ` Andrew Lunn
2014-05-24 11:44 ` [B.A.T.M.A.N.] [PATCH 07/10] alfred: Use strncpy instead of strcpy for string copy Sven Eckelmann
2014-05-24 11:44 ` [B.A.T.M.A.N.] [PATCH 08/10] alfred: Free hash iterator when breaking out of loop Sven Eckelmann
2014-05-24 11:44 ` [B.A.T.M.A.N.] [PATCH 09/10] alfred: Handle fcntl error return codes Sven Eckelmann
2014-05-24 11:44 ` [B.A.T.M.A.N.] [PATCH 10/10] alfred: Fix length check for push_data Sven Eckelmann
2014-05-27 14:08 ` [B.A.T.M.A.N.] [PATCH 01/10] batadv-vis: Avoid memory leak after failed realloc Simon Wunderlich

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