From patchwork Sat May 24 11:44:06 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sven Eckelmann X-Patchwork-Id: 4027 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=79.140.41.39; helo=v3-1039.vlinux.de; envelope-from=sven@narfation.org; receiver=b.a.t.m.a.n@lists.open-mesh.org Received: from v3-1039.vlinux.de (narfation.org [79.140.41.39]) by open-mesh.org (Postfix) with ESMTPS id 23187600B5A for ; Sat, 24 May 2014 13:44:22 +0200 (CEST) Received: from sven-desktop.home.narfation.org (unknown [IPv6:2a02:3100:2601:a5fd:f66d:4ff:fe4e:ec03]) by v3-1039.vlinux.de (Postfix) with ESMTPSA id 6CCC11100DB; Sat, 24 May 2014 13:44:22 +0200 (CEST) From: Sven Eckelmann To: b.a.t.m.a.n@lists.open-mesh.org Date: Sat, 24 May 2014 13:44:06 +0200 Message-Id: <1400931855-7961-1-git-send-email-sven@narfation.org> X-Mailer: git-send-email 2.0.0.rc2 Cc: Sven Eckelmann Subject: [B.A.T.M.A.N.] [PATCH 01/10] batadv-vis: Avoid memory leak after failed realloc X-BeenThere: b.a.t.m.a.n@lists.open-mesh.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: The list for a Better Approach To Mobile Ad-hoc Networking List-Id: The list for a Better Approach To Mobile Ad-hoc Networking List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 24 May 2014 11:44:23 -0000 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 --- 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; }