From 48c9f6196ffb387f423c7f711df079baffeed6d6 Mon Sep 17 00:00:00 2001 From: Robin Hsu Date: Thu, 7 Nov 2019 13:44:08 +0800 Subject: [PATCH] libsparse: fix memory leak in output_file_close() Fix memory leak in output_file_close(): Arguable whose resposibility to free structs allocated inside a file struct (the caller or the library function), but the following protocol (i.e. this fix) would better prevent memory leaks: 1) output_file_close() function will free those structs inside a file struct, right before closing the file. * Note: those structs are originally allocated by other libsparse function. 2) If the caller wants to clean up those struct inside a file struct, it may. Just free those structs, and set the pointer to NULL. Afterward, when file_close()'s are called, they won't be free'ed twice. Bug: 142483439 Test: verified by script MemLeak_LastCmd.sh (attached in Bugnizer) Change-Id: I8bb9f7c9f7d19268663e2830d1a90d27bd5f99bd Signed-off-by: Robin Hsu --- libsparse/output_file.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libsparse/output_file.cpp b/libsparse/output_file.cpp index c5c49605d..e35cb0d0e 100644 --- a/libsparse/output_file.cpp +++ b/libsparse/output_file.cpp @@ -493,6 +493,10 @@ static struct sparse_file_ops normal_file_ops = { void output_file_close(struct output_file* out) { out->sparse_ops->write_end_chunk(out); + free(out->zero_buf); + free(out->fill_buf); + out->zero_buf = nullptr; + out->fill_buf = nullptr; out->ops->close(out); }