# [PATCH] fsck: fix leak when traversing trees

2 messages from 2018-01-20 to 2018-01-23. Participants: Eric Wong, Junio C Hamano.
Thread: https://gitlist.dev/t/47653

## Eric Wong, 2018-01-20 07:43

Subject: [PATCH] fsck: fix leak when traversing trees
Message-ID: <20180120074351.GA12439@whir>
URL: https://gitlist.dev/e/20180120074351.GA12439%40whir

```
While fsck_walk/fsck_walk_tree/parse_tree populates "struct tree"
idempotently, it is still up to the fsck_walk caller to call
free_tree_buffer.

Fixes: ad2db4030e42890e ("fsck: remove redundant parse_tree() invocation")

Signed-off-by: Eric Wong <e@80x24.org>
---
 These APIs could probably be made to be less error-prone,
 but at least this stops my little machine from OOM-ing.

 builtin/fsck.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/builtin/fsck.c b/builtin/fsck.c
index 04846d46f9..92ce775a74 100644
--- a/builtin/fsck.c
+++ b/builtin/fsck.c
@@ -171,7 +171,13 @@ static void mark_object_reachable(struct object *obj)
 
 static int traverse_one_object(struct object *obj)
 {
-	return fsck_walk(obj, obj, &fsck_walk_options);
+	int result = fsck_walk(obj, obj, &fsck_walk_options);
+
+	if (obj->type == OBJ_TREE) {
+		struct tree *tree = (struct tree *)obj;
+		free_tree_buffer(tree);
+	}
+	return result;
 }
 
 static int traverse_reachable(void)
-- 
EW

```

## Junio C Hamano, 2018-01-23 18:18

Subject: Re: [PATCH] fsck: fix leak when traversing trees
Message-ID: <xmqqpo60iglr.fsf@gitster.mtv.corp.google.com>
URL: https://gitlist.dev/e/xmqqpo60iglr.fsf%40gitster.mtv.corp.google.com
In-Reply-To: <20180120074351.GA12439@whir>

```
Eric Wong <e@80x24.org> writes:

> While fsck_walk/fsck_walk_tree/parse_tree populates "struct tree"
> idempotently, it is still up to the fsck_walk caller to call
> free_tree_buffer.
>
> Fixes: ad2db4030e42890e ("fsck: remove redundant parse_tree() invocation")

Yup, we can see that that commit did stop freeing the tree buffer.
Thanks.

>
> Signed-off-by: Eric Wong <e@80x24.org>
> ---
>  These APIs could probably be made to be less error-prone,
>  but at least this stops my little machine from OOM-ing.
>
>  builtin/fsck.c | 8 +++++++-
>  1 file changed, 7 insertions(+), 1 deletion(-)
>
> diff --git a/builtin/fsck.c b/builtin/fsck.c
> index 04846d46f9..92ce775a74 100644
> --- a/builtin/fsck.c
> +++ b/builtin/fsck.c
> @@ -171,7 +171,13 @@ static void mark_object_reachable(struct object *obj)
>  
>  static int traverse_one_object(struct object *obj)
>  {
> -	return fsck_walk(obj, obj, &fsck_walk_options);
> +	int result = fsck_walk(obj, obj, &fsck_walk_options);
> +
> +	if (obj->type == OBJ_TREE) {
> +		struct tree *tree = (struct tree *)obj;
> +		free_tree_buffer(tree);
> +	}
> +	return result;
>  }
>  
>  static int traverse_reachable(void)

```
