[PATCH 4/6] cgit_print_tree(): Free curr_rev after usage

Lukas Fleischer cgit at cryptocrack.de
Mon Mar 4 13:25:36 CET 2013


Fixes following memory leak seen with "PATH_INFO=/cgit/tree/":

    ==15715== 7 bytes in 1 blocks are definitely lost in loss record 4 of 51
    ==15715==    at 0x4C2C04B: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
    ==15715==    by 0x56F2DF1: strdup (in /usr/lib/libc-2.17.so)
    ==15715==    by 0x46CAA8: xstrdup (wrapper.c:35)
    ==15715==    by 0x418A4C: cgit_print_tree (ui-tree.c:274)
    ==15715==    by 0x407D91: tree_fn (cmd.c:131)
    ==15715==    by 0x405E16: process_request (cgit.c:574)
    ==15715==    by 0x4074C8: cache_process (cache.c:322)
    ==15715==    by 0x406C4F: main (cgit.c:872)

Signed-off-by: Lukas Fleischer <cgit at cryptocrack.de>
---
 ui-tree.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/ui-tree.c b/ui-tree.c
index 561f9e7..d74f048 100644
--- a/ui-tree.c
+++ b/ui-tree.c
@@ -271,7 +271,6 @@ void cgit_print_tree(const char *rev, char *path)
 	if (!rev)
 		rev = ctx.qry.head;
 
-	walk_tree_ctx.curr_rev = xstrdup(rev);
 	if (get_sha1(rev, sha1)) {
 		cgit_print_error(fmt("Invalid revision name: %s", rev));
 		return;
@@ -282,12 +281,16 @@ void cgit_print_tree(const char *rev, char *path)
 		return;
 	}
 
+	walk_tree_ctx.curr_rev = xstrdup(rev);
+
 	if (path == NULL) {
 		ls_tree(commit->tree->object.sha1, NULL, &walk_tree_ctx);
+		free(walk_tree_ctx.curr_rev);
 		return;
 	}
 
 	read_tree_recursive(commit->tree, "", 0, 0, &paths, walk_tree, &walk_tree_ctx);
 	if (walk_tree_ctx.state == 1)
 		ls_tail();
+	free(walk_tree_ctx.curr_rev);
 }
-- 
1.8.2.rc0.247.g811e0c0





More information about the CGit mailing list