Re: [PATCH 2/2] read-cache.c: fix a memleak in add_to_index

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Stefan Beller <sbeller@xxxxxxxxxx> writes:

> Signed-off-by: Stefan Beller <sbeller@xxxxxxxxxx>
> ---
>
>  I have reread your remarks from the weekend, and I agree
>  this looks more intuitive. Thanks for pointing out the subtle details
>  to make programming an art!

Heh, Our mails crossed, I guess.  I've done this myself like this

-- >8 --
From: Junio C Hamano <gitster@xxxxxxxxx>
Date: Mon, 23 Mar 2015 10:58:00 -0700
Subject: [PATCH] add_to_index(): free unused cache-entry

We allocate a cache-entry pretty early in the function and then
decide either not to do anything when we are pretending to add, or
add it and then get an error (another possibility is obviously to
succeed).

When pretending or failing to add, we forgot to free the
cache-entry.

Noticed during a discussion on Stefan's patch to change the coding
style without fixing the issue ;-)

Signed-off-by: Junio C Hamano <gitster@xxxxxxxxx>
---
 read-cache.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/read-cache.c b/read-cache.c
index 60abec6..5b922fd5 100644
--- a/read-cache.c
+++ b/read-cache.c
@@ -707,9 +707,11 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
 		    ce->ce_mode == alias->ce_mode);
 
 	if (pretend)
-		;
-	else if (add_index_entry(istate, ce, add_option))
-		return error("unable to add %s to index",path);
+		free(ce);
+	else if (add_index_entry(istate, ce, add_option)) {
+		free(ce);
+		return error("unable to add %s to index", path);
+	}
 	if (verbose && !was_same)
 		printf("add '%s'\n", path);
 	return 0;
-- 
2.3.3-454-g85aa98f

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]