[01/10] batadv-vis: Avoid memory leak after failed realloc

Message ID 1400931855-7961-1-git-send-email-sven@narfation.org (mailing list archive)
State Accepted, archived
Commit 0ad384e11ed039d4c3025a7eaf19fe6bcfd41acf
Headers

Commit Message

Sven Eckelmann May 24, 2014, 11:44 a.m. UTC
  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(-)
  

Comments

Simon Wunderlich May 27, 2014, 2:08 p.m. UTC | #1
> 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>

The whole series has been applied with two minor fixes:
 * fixed a typo: alfred-gpsd: Fix altitute verification check -> altitude
 * fixed sign of length calculation in alfred: Fix length check for push_data

Thanks!
    Simon
  

Patch

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;
 	}