From c32d7bae15c15155f9ab0281ae0496952795e73c Mon Sep 17 00:00:00 2001
From: Andreas Gampe <agampe@google.com>
Date: Thu, 27 Apr 2017 18:39:02 -0700
Subject: [PATCH] fc_sort: Fix leaks

Use the getline API correctly: keep a single buffer as long as
possible, and let the callee handle re-allocation. Move the final
free out of the loop.

Release the head of the linked list.

Bug: 37757586
Test: ASAN_OPTIONS= SANITIZE_HOST=address mmma system/sepolicy
Change-Id: I42424acba7cd68c1b9a7a43e916a421ac3e253f7
---
 tools/fc_sort/fc_sort.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/tools/fc_sort/fc_sort.c b/tools/fc_sort/fc_sort.c
index 5561288ad..9a3a3ee14 100644
--- a/tools/fc_sort/fc_sort.c
+++ b/tools/fc_sort/fc_sort.c
@@ -350,6 +350,7 @@ int main(int argc, char *argv[])
 
 	/* Parse the file into a file_context linked list. */
 	line_buf = NULL;
+	buf_len = 0;
 
 	while ( getline(&line_buf, &buf_len, in_file) != -1 ){
 		line_len = strlen(line_buf);
@@ -478,15 +479,13 @@ int main(int argc, char *argv[])
 		current->next = temp;
 		current = current->next;
 		lines++;
-
-
-		free(line_buf);
-		line_buf = NULL;
 	}
+	free(line_buf);
 	fclose(in_file);
 
 	/* Create the bucket linked list from the earlier linked list. */
 	current = head->next;
+	free(head);
 	bcurrent = master =
 	    (file_context_bucket_t *)
 	    malloc(sizeof(file_context_bucket_t));
-- 
GitLab