Skip to contentSkip to conversation

Fixing memory leaks

PATCH In review Stefan Beller 3 participants 44 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

cover Fixing memory leaks

So here comes another bunch of mem leak fixes. While I consider the first 10 patches a pretty safe bet, the last 5 hopefully spark a discussion if we want patches which just clean up before the program ends.

Stefan Beller (15):
read-cache: fix memleak
read-cache: Improve readability
read-cache: free cache entry in add_to_index in case of early return
update-index: fix a memleak
builtin/apply.c: fix a memleak
merge-blobs.c: Fix a memleak
merge-recursive: fix memleaks
http-push: Remove unneeded cleanup
http: release the memory of a http pack request as well commit.c: fix a memory leak
builtin/check-attr: fix a memleak
builtin/merge-base: fix memleak
builtin/unpack-file: fix a memleak
builtin/cat-file: free memleak
ls-files: fix a memleak

 builtin/apply.c        |  1 +
 builtin/cat-file.c     |  1 +
 builtin/check-attr.c   |  2 ++
 builtin/commit.c       |  6 ++++--
 builtin/ls-files.c     |  1 +
 builtin/merge-base.c   |  1 +
 builtin/unpack-file.c  |  1 +
 builtin/update-index.c |  1 +
 http-push.c            |  1 -
 http.c                 |  1 +
 merge-blobs.c          |  4 +++-
 merge-recursive.c      |  3 +++
 read-cache.c           | 13 ++++++++-----
 13 files changed, 27 insertions(+), 9 deletions(-)
Signature · 1 line
2.3.0.81.gc37f363
#1 lore raw

PATCH 1/15 read-cache: fix memleak

ce is allocated in make_cache_entry and should be freed if it is not used any more. refresh_cache_entry as a wrapper around refresh_cache_ent will either return ce or a new updated cache entry which is allocated to new memory. In that case we need to free ce ourselfs.

---
 read-cache.c | 2 ++
 1 file changed, 2 insertions(+)
read-cache.c +2 -0
index 8d71860..f72ea9f 100644
@@ -747,6 +747,8 @@ struct cache_entry *make_cache_entry(unsigned int mode,
 		free(ce);
 		return NULL;
 	} else {
+		if (ret != ce)
+			free(ce);
 		return ret;
 	}
 }
Signature · 1 line
2.3.0.81.gc37f363
#2 lore raw

Re: [PATCH 01/15] read-cache: fix memleak

Quoted 15 lines · Stefan Beller

ce is allocated in make_cache_entry and should be freed if it is not used any more. refresh_cache_entry as a wrapper around refresh_cache_ent will either return ce or a new updated cache entry which is allocated to new memory. In that case we need to free ce ourselfs.

---
 read-cache.c | 2 ++
 1 file changed, 2 insertions(+)
read-cache.c +0 -0
index 8d71860..f72ea9f 100644

read-cache.c struct cache_entry *make_cache_entry(unsigned int mode, · line 754

Quoted 8 lines
read-cache.c +2 -0
@@ -747,6 +747,8 @@ struct cache_entry *make_cache_entry(unsigned int mode,
 		free(ce);
 		return NULL;
 	} else {
+		if (ret != ce)
+			free(ce);
 		return ret;
 	}
 }

Good, I vaguely recall that we did something similar in another codepath that forgot the fact that refresh_cache_entry() may make the incoming ce unnecessary.

As the rule is "if ret is different from ce, then ce must be freed" in this codepath, I wonder if this is easier to read:

	ret = refresh_cache_entry(ce, ...);
        if (ret != ce)
        	free(ce);
	return ret;
#8 lore raw

PATCH 2/15 read-cache: Improve readability

In reply to Stefan Beller
---
 read-cache.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
read-cache.c +1 -3
index f72ea9f..769897e 100644
@@ -703,9 +703,7 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
 		    !hashcmp(alias->sha1, ce->sha1) &&
 		    ce->ce_mode == alias->ce_mode);
 
-	if (pretend)
-		;
-	else if (add_index_entry(istate, ce, add_option))
+	if (!pretend && add_index_entry(istate, ce, add_option))
 		return error("unable to add %s to index",path);
 	if (verbose && !was_same)
 		printf("add '%s'\n", path);
Signature · 1 line
2.3.0.81.gc37f363
#9 lore raw

Re: [PATCH 02/15] read-cache: Improve readability

Quoted 10 lines · Stefan Beller
---
 read-cache.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
read-cache.c +0 -0
index f72ea9f..769897e 100644

read-cache.c int add_to_index(struct index_state *istate, const char *path, struct stat *st, · line 709

Quoted 10 lines
read-cache.c +1 -3
@@ -703,9 +703,7 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
 		    !hashcmp(alias->sha1, ce->sha1) &&
 		    ce->ce_mode == alias->ce_mode);
 
-	if (pretend)
-		;
-	else if (add_index_entry(istate, ce, add_option))
+	if (!pretend && add_index_entry(istate, ce, add_option))
 		return error("unable to add %s to index",path);
 	if (verbose && !was_same)
 		printf("add '%s'\n", path);

I have a moderately strong feeling against this change, as the code was done this way quite deliberately to keep it readable, namely, to avoid using && to concatenate two boolean expressions that are in totally different class inside condition part of if/while, where A is a precondition guard for B (i.e. you cannot evaluate B unless A holds) and B is called primarily for its side effect. The problem is that, once you start liberally doing

	if (A && B && C && D ...)

with booleans with mixed semantics (guards and actions), it will quickly get harder to tell which one is which.

I could have written it as

	if (!pretend) {
        	if (add_index_entry(...))
			return error(...);
	}

and that would have been just as readable as the original; it clearly separates the guard (i.e. only do the add-index thing when we are not pretending) and the operation that is done for the side effect.

But I find the original tells you "if pretend mode, do *nothing*" and "otherwise, try add_index_entry() and act on its error" very clearly. Of course, I am biased as the original is my code from 38ed1d89 ("git-add -n -u" should not add but just report, 2008-05-21).

FYI, between preference and taste, I'd say this one is much closer to the latter than the former.

By the way, aren't we leaking ce when we are merely pretending?

#10 lore raw

Re: [PATCH 02/15] read-cache: Improve readability

Quoted 36 lines · Junio C Hamano
Quoted 10 lines · Stefan Beller
---
 read-cache.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
read-cache.c +0 -0
index f72ea9f..769897e 100644

read-cache.c int add_to_index(struct index_state *istate, const char *path, struct stat *st, · line 709

Quoted 10 lines
read-cache.c +1 -3
@@ -703,9 +703,7 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
                  !hashcmp(alias->sha1, ce->sha1) &&
                  ce->ce_mode == alias->ce_mode);
 
-     if (pretend)
-             ;
-     else if (add_index_entry(istate, ce, add_option))
+     if (!pretend && add_index_entry(istate, ce, add_option))
              return error("unable to add %s to index",path);
      if (verbose && !was_same)
              printf("add '%s'\n", path);

I have a moderately strong feeling against this change, as the code was done this way quite deliberately to keep it readable, namely, to avoid using && to concatenate two boolean expressions that are in totally different class inside condition part of if/while, where A is a precondition guard for B (i.e. you cannot evaluate B unless A holds) and B is called primarily for its side effect. The problem is that, once you start liberally doing

        if (A && B && C && D ...)

with booleans with mixed semantics (guards and actions), it will quickly get harder to tell which one is which.

Quoted 6 lines

I could have written it as

        if (!pretend) {
                if (add_index_entry(...))
                        return error(...);
        }

This makes sense to point out the different semantics to me. Maybe I have read too much of the refs code lately as there we have these long chains which combine precondition with error checking. :/ That's why I thought it would be global to git to not care much about this semantics distinction.

Quoted 12 lines

and that would have been just as readable as the original; it clearly separates the guard (i.e. only do the add-index thing when we are not pretending) and the operation that is done for the side effect.

But I find the original tells you "if pretend mode, do *nothing*" and "otherwise, try add_index_entry() and act on its error" very clearly. Of course, I am biased as the original is my code from 38ed1d89 ("git-add -n -u" should not add but just report, 2008-05-21).

Quoted 4 lines

FYI, between preference and taste, I'd say this one is much closer to the latter than the former.

By the way, aren't we leaking ce when we are merely pretending?

Yes we are, that's how I found this spot. (coverity pointed out ce was leaking, so I was refactoring to actually make it easier to fix it, and then heavily reordered the patch series afterwards. That spot was forgotten apparently.

Quoted 2 lines
#12 lore raw

PATCH 3/15 read-cache: free cache entry in add_to_index in case of early return

In reply to Stefan Beller

This frees ce would be leaking in the error path.

Additionally a free is moved towards the return. This helps code readability as we often have this pattern of freeing resources just before return/exit and not in between the code.

---
 read-cache.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
read-cache.c +5 -2
index 769897e..935f91a 100644
@@ -681,15 +681,18 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
 	alias = index_file_exists(istate, ce->name, ce_namelen(ce), ignore_case);
 	if (alias && !ce_stage(alias) && !ie_match_stat(istate, alias, st, ce_option)) {
 		/* Nothing changed, really */
-		free(ce);
 		if (!S_ISGITLINK(alias->ce_mode))
 			ce_mark_uptodate(alias);
 		alias->ce_flags |= CE_ADDED;
+
+		free(ce);
 		return 0;
 	}
 	if (!intent_only) {
-		if (index_path(ce->sha1, path, st, HASH_WRITE_OBJECT))
+		if (index_path(ce->sha1, path, st, HASH_WRITE_OBJECT)) {
+			free(ce);
 			return error("unable to index file %s", path);
+		}
 	} else
 		set_object_name_for_intent_to_add_entry(ce);
 
Signature · 1 line
2.3.0.81.gc37f363
#13 lore raw

Re: [PATCH 03/15] read-cache: free cache entry in add_to_index in case of early return

Quoted 1 line · Stefan Beller

This frees ce would be leaking in the error path.

At this point ce is not yet added to the index, so it is clear it is safe to free it---otherwise we will leak it. Good.

Quoted 1 line

Additionally a free is moved towards the return.

I am on the fence on this one between two schools and do not have a strong preference. One school is to free as soon as you know you do not need it, which is a valid stance to take. Another is, as you did, not to care about the minimum necessary lifetime of the storage and free them all at the end, which is also valid. Technically, the former could be more performant while the latter is easier on the eyes.

#14 lore raw

Re: [PATCH 03/15] read-cache: free cache entry in add_to_index in case of early return

Quoted 9 lines · Junio C Hamano
Quoted 1 line · Stefan Beller

This frees ce would be leaking in the error path.

At this point ce is not yet added to the index, so it is clear it is safe to free it---otherwise we will leak it. Good.

Quoted 1 line

Additionally a free is moved towards the return.

Quoted 7 lines

I am on the fence on this one between two schools and do not have a strong preference. One school is to free as soon as you know you do not need it, which is a valid stance to take. Another is, as you did, not to care about the minimum necessary lifetime of the storage and free them all at the end, which is also valid. Technically, the former could be more performant while the latter is easier on the eyes.

I only recall to have seen the latter school so far, which is why I made the change in the first place assuming the school of freeing ASAP has no strong supporters inside the git community.

I can resend the patch dropping the reordering, if you prefer.

#16 lore raw

PATCH 4/15 update-index: fix a memleak

In reply to Stefan Beller

old is not used outside the loop and would get lost once we reach the goto.

---
 builtin/update-index.c | 1 +
 1 file changed, 1 insertion(+)
builtin/update-index.c +1 -0
index 5878986..6271b54 100644
@@ -584,6 +584,7 @@ static int do_reupdate(int ac, const char **av,
 		path = xstrdup(ce->name);
 		update_one(path);
 		free(path);
+		free(old);
 		if (save_nr != active_nr)
 			goto redo;
 	}
Signature · 1 line
2.3.0.81.gc37f363
#17 lore raw

Re: [PATCH 04/15] update-index: fix a memleak

Quoted 13 lines · Stefan Beller

old is not used outside the loop and would get lost once we reach the goto.

---
 builtin/update-index.c | 1 +
 1 file changed, 1 insertion(+)
builtin/update-index.c +0 -0
index 5878986..6271b54 100644

builtin/update-index.c static int do_reupdate(int ac, const char **av, · line 587

Quoted 4 lines
builtin/update-index.c +1 -0
@@ -584,6 +584,7 @@ static int do_reupdate(int ac, const char **av,
 		path = xstrdup(ce->name);
 		update_one(path);
 		free(path);
+		free(old);

The change looks trivially correct.

A tangent; I wonder if we want to make path a strbuf that is declared in the function scope, reset (not released) at each iteration of the loop and released after the loop; keep allocating and freeing a small piece of string all the time feels somewhat wasteful.

Also, we might want to add to the "Be careful" comment to describe why this cannot just be a call to "update_one(ce->name)" that does not use "path".

#20 lore raw

PATCH 5/15 builtin/apply.c: fix a memleak

In reply to Stefan Beller

oldlines is allocated earlier in the function and also freed on the successful code path.

---
 builtin/apply.c | 1 +
 1 file changed, 1 insertion(+)
builtin/apply.c +1 -0
index 65b97ee..e152c4d 100644
@@ -2776,6 +2776,7 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
 		default:
 			if (apply_verbosely)
 				error(_("invalid start of line: '%c'"), first);
+			free(oldlines);
 			return -1;
 		}
 		if (added_blank_line) {
Signature · 1 line
2.3.0.81.gc37f363
#21 lore raw

Re: [PATCH 05/15] builtin/apply.c: fix a memleak

Quoted 13 lines · Stefan Beller

oldlines is allocated earlier in the function and also freed on the successful code path.

---
 builtin/apply.c | 1 +
 1 file changed, 1 insertion(+)
builtin/apply.c +0 -0
index 65b97ee..e152c4d 100644

builtin/apply.c static int apply_one_fragment(struct image *img, struct fragment *frag, · line 2780

Quoted 5 lines
builtin/apply.c +1 -0
@@ -2776,6 +2776,7 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
 		default:
 			if (apply_verbosely)
 				error(_("invalid start of line: '%c'"), first);
+			free(oldlines);
 			return -1;

Good.

By the way, aren't the following also leaking here?

- the strbuf newlines that starts out as "size"
- line[] arrays of preimage and postimage

Quoted 2 lines
builtin/apply.c +0 -0
 		}
 		if (added_blank_line) {
#24 lore raw

PATCH 6/15 merge-blobs.c: Fix a memleak

In reply to Stefan Beller
---
 merge-blobs.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
merge-blobs.c +3 -1
index 57211bc..7abb894 100644
@@ -14,8 +14,10 @@ static int fill_mmfile_blob(mmfile_t *f, struct blob *obj)
 	buf = read_sha1_file(obj->object.sha1, &type, &size);
 	if (!buf)
 		return -1;
-	if (type != OBJ_BLOB)
+	if (type != OBJ_BLOB) {
+		free(buf);
 		return -1;
+	}
 	f->ptr = buf;
 	f->size = size;
 	return 0;
Signature · 1 line
2.3.0.81.gc37f363
#25 lore raw

PATCH 7/15 merge-recursive: fix memleaks

In reply to Stefan Beller

Usually when using string lists it looks like:

    struct string_list a = STRING_LIST_INIT_NODUP;
    // do stuff with a such as
    string_list_insert(&a, "test string");
    print_string_list(&a, "test prefix");
    // Cleaning up works on everything inside the struct, not on the
    // struct itself:
    string_list_clear(&a);

But as we deal with the pointers to the string lists directly, we also need to free the actual struct.

---
 merge-recursive.c | 3 +++
 1 file changed, 3 insertions(+)
merge-recursive.c +3 -0
index 771f5e2..1c9c30d 100644
@@ -1858,6 +1858,9 @@ int merge_trees(struct merge_options *o,
 		string_list_clear(re_head, 0);
 		string_list_clear(entries, 1);
 
+		free(re_merge);
+		free(re_head);
+		free(entries);
 	}
 	else
 		clean = 1;
Signature · 1 line
2.3.0.81.gc37f363
#26 lore raw

Re: [PATCH 07/15] merge-recursive: fix memleaks

Quoted 11 lines · Stefan Beller

Usually when using string lists it looks like:

    struct string_list a = STRING_LIST_INIT_NODUP;
    // do stuff with a such as
    string_list_insert(&a, "test string");
    print_string_list(&a, "test prefix");
    // Cleaning up works on everything inside the struct, not on the
    // struct itself:
    string_list_clear(&a);

But as we deal with the pointers to the string lists directly, we also need to free the actual struct.

In other words, these two were allocated for the sole use of this fuction by get_renames(), so this function is responsible for freeing it?

Sounds sensible.

Quoted 19 lines
---
 merge-recursive.c | 3 +++
 1 file changed, 3 insertions(+)
merge-recursive.c +3 -0
index 771f5e2..1c9c30d 100644
@@ -1858,6 +1858,9 @@ int merge_trees(struct merge_options *o,
 		string_list_clear(re_head, 0);
 		string_list_clear(entries, 1);
 
+		free(re_merge);
+		free(re_head);
+		free(entries);
 	}
 	else
 		clean = 1;
#27 lore raw

PATCH 8/15 http-push: Remove unneeded cleanup

In reply to Stefan Beller

preq is NULL as the condition the line before dictates. And the cleanup function release_http_pack_request is not null pointer safe.

---
 http-push.c | 1 -
 1 file changed, 1 deletion(-)
http-push.c +0 -1
index bfb1c96..c98dad2 100644
@@ -316,7 +316,6 @@ static void start_fetch_packed(struct transfer_request *request)
 
 	preq = new_http_pack_request(target, repo->url);
 	if (preq == NULL) {
-		release_http_pack_request(preq);
 		repo->can_update_info_refs = 0;
 		return;
 	}
Signature · 1 line
2.3.0.81.gc37f363
#28 lore raw

PATCH 9/15 http: release the memory of a http pack request as well

In reply to Stefan Beller

The cleanup function is used in 4 places now and it's always safe to free up the memory as well.

---
 http.c | 1 +
 1 file changed, 1 insertion(+)
http.c +1 -0
index 9c825af..4b179f6 100644
@@ -1462,6 +1462,7 @@ void release_http_pack_request(struct http_pack_request *preq)
 	}
 	preq->slot = NULL;
 	free(preq->url);
+	free(preq);
 }
 
 int finish_http_pack_request(struct http_pack_request *preq)
Signature · 1 line
2.3.0.81.gc37f363
#32 lore raw

PATCH 10/15 commit.c: fix a memory leak

In reply to Stefan Beller
---
 builtin/commit.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
builtin/commit.c +4 -2
index 961e467..da79ac4 100644
@@ -229,7 +229,7 @@ static int commit_index_files(void)
 static int list_paths(struct string_list *list, const char *with_tree,
 		      const char *prefix, const struct pathspec *pattern)
 {
-	int i;
+	int i, ret;
 	char *m;
 
 	if (!pattern->nr)
@@ -256,7 +256,9 @@ static int list_paths(struct string_list *list, const char *with_tree,
 			item->util = item; /* better a valid pointer than a fake one */
 	}
 
-	return report_path_error(m, pattern, prefix);
+	ret = report_path_error(m, pattern, prefix);
+	free(m);
+	return ret;
 }
 
 static void add_remove_files(struct string_list *list)
Signature · 1 line
2.3.0.81.gc37f363
#33 lore raw

Re: [PATCH 10/15] commit.c: fix a memory leak

Quoted 19 lines · Stefan Beller
---
 builtin/commit.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
builtin/commit.c +1 -1
index 961e467..da79ac4 100644
@@ -229,7 +229,7 @@ static int commit_index_files(void)
 static int list_paths(struct string_list *list, const char *with_tree,
 		      const char *prefix, const struct pathspec *pattern)
 {
-	int i;
+	int i, ret;
 	char *m;
 
 	if (!pattern->nr)

builtin/commit.c static int list_paths(struct string_list *list, const char *with_tree, · line 262

Quoted 8 lines
builtin/commit.c +3 -1
@@ -256,7 +256,9 @@ static int list_paths(struct string_list *list, const char *with_tree,
 			item->util = item; /* better a valid pointer than a fake one */
 	}
 
-	return report_path_error(m, pattern, prefix);
+	ret = report_path_error(m, pattern, prefix);
+	free(m);
+	return ret;
 }

Looks correct.

A tangent. We may want to move report_path_error() to somewhere more "common"-ish, like dir.c which is where we have bulk of pathspec matching infrastructure. Seeing the function used from builtin/ls-files.c makes me feel dirty.

A further tangent (Duy Cc'ed for this point). We might want to rethink the interface to ce_path_match() and report_path_error() so that we do not have to do a separate allocation of "has this pathspec been used?" array. This was a remnant from the olden days back when pathspec were mere "const char **" where we did not have any room to add per-item bit---these days pathspec is repreasented as an array of "struct pathspec" and we can afford to add a bit to the structure---which will make this kind of leak much less likely to happen.

#37 lore raw

PATCH 11/15 builtin/check-attr: fix a memleak

In reply to Stefan Beller

---

Notes:
I do not quite recall a discussion about such fixes when I started doing these fixes like 2 years ago.

As this is a main function of a subcommand the freed memory is likely to have no impact as the process is done soon, so then it gets freed by the OS which is likey to be faster as the OS frees the whole pages of the process. Also there is no expected memory shortage as the process is going to be done soon as opposed to fixing mem leaks early in the process.

An upside of fixes like this one is however to make code analysis tools produce less noise, so narrowing down the *real* issue may be easier.

I wonder if we could have a 'weak' free which does nothing if git is compiled regularly and actually frees the memory if it is build with a flag to tell it to do so. This would help finding the real issues as the noise goes down and it would still be 'fast' as it could be when compiled for productive use.

On the other hand I don't like to have another 'invented here again' systemcall-ish function as it would clutter the code and you'd have to remember to use that weak free.

I dunno.

 builtin/check-attr.c | 2 ++
 1 file changed, 2 insertions(+)
builtin/check-attr.c +2 -0
index 21d2bed..fa96356 100644
@@ -182,5 +182,7 @@ int cmd_check_attr(int argc, const char **argv, const char *prefix)
 			check_attr(prefix, cnt, check, argv[i]);
 		maybe_flush_or_die(stdout, "attribute to stdout");
 	}
+
+	free(check);
 	return 0;
 }
Signature · 1 line
2.3.0.81.gc37f363
#38 lore raw

Re: [PATCH 11/15] builtin/check-attr: fix a memleak

Quoted 7 lines · Stefan Beller

---

Notes:
I do not quite recall a discussion about such fixes when I started doing these fixes like 2 years ago.

Quoted 5 lines

As this is a main function of a subcommand the freed memory is likely to have no impact as the process is done soon, so then it gets freed by the OS which is likey to be faster as the OS frees the whole pages of the process.

You seem to understand it well without "recalling" ;-)

As you said, unless the false hits from valgrind on these overwhelm the real issues we are looking for, it is usually not worth the patch churn to fix these. Another valid reason is if we plan to libify a function that used to be a top-level.

#39 lore raw

PATCH 12/15 builtin/merge-base: fix memleak

In reply to Stefan Beller

---

Notes:
see discussion on previous patch.

 builtin/merge-base.c | 1 +
 1 file changed, 1 insertion(+)
builtin/merge-base.c +1 -0
index 08a8217..4a8ff02 100644
@@ -22,6 +22,7 @@ static int show_merge_base(struct commit **rev, int rev_nr, int show_all)
 		result = result->next;
 	}
 
+	free(rev);
 	return 0;
 }
 
Signature · 1 line
2.3.0.81.gc37f363
#40 lore raw

PATCH 13/15 builtin/unpack-file: fix a memleak

In reply to Stefan Beller

---

Notes:
This is for discussion again,
see 2nd previous patch.

However why do we close the fd when we know
the program ends soon? So let's also
free the memory.

 builtin/unpack-file.c | 1 +
 1 file changed, 1 insertion(+)
builtin/unpack-file.c +1 -0
index 1920029..ea5767e 100644
@@ -17,6 +17,7 @@ static char *create_temp_file(unsigned char *sha1)
 	if (write_in_full(fd, buf, size) != size)
 		die_errno("unable to write temp-file");
 	close(fd);
+	free(buf);
 	return path;
 }
 
Signature · 1 line
2.3.0.81.gc37f363
#41 lore raw

PATCH 14/15 builtin/cat-file: free memleak

In reply to Stefan Beller

---

Notes:
see discussion

 builtin/cat-file.c | 1 +
 1 file changed, 1 insertion(+)
builtin/cat-file.c +1 -0
index df99df4..8de6b0d 100644
@@ -104,6 +104,7 @@ static int cat_one_file(int opt, const char *exp_type, const char *obj_name)
 		die("git cat-file %s: bad file", obj_name);
 
 	write_or_die(1, buf, size);
+	free(buf);
 	return 0;
 }
 
Signature · 1 line
2.3.0.81.gc37f363
#42 lore raw

PATCH 15/15 ls-files: fix a memleak

In reply to Stefan Beller

---

Notes:
see discussion

 builtin/ls-files.c | 1 +
 1 file changed, 1 insertion(+)
builtin/ls-files.c +1 -0
index 914054d..306defa 100644
@@ -590,6 +590,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)
 		if (bad)
 			fprintf(stderr, "Did you forget to 'git add'?\n");
 
+		free(ps_matched);
 		return bad ? 1 : 0;
 	}
 
Signature · 1 line
2.3.0.81.gc37f363
#11 lore raw

Re: [PATCH 02/15] read-cache: Improve readability

In reply to Stefan Beller
Quoted 3 lines · Stefan Beller

Maybe I have read too much of the refs code lately as there we have these long chains which combine precondition with error checking.

Of course, things are not so black-and-white in the real world.

You can also take an extreme stance and view

	if (!pretend && do_it_and_report_error())
        	error(...);

differently. I would explain that what the whole thing is trying to achieve as "'do it' part is the primary thing we want to do, but it only can be done when we are not pretending, and we show an error when the 'do it' part fails." and would suggest structuring it more like this:

	if (pretend)
        	; /* nothing */
	else if (do_it_and_report_error())
        	error(...);

or

	if (!pretend) {
        	if (do_it_and_report_error())
                	error(...);
	}

But you could say "The final objective of the whole thing is to show an error message, but if we are pretending or if our attempt to 'do it' succeeds, we do not have to show the error", and such a view may make sense depending on what that 'do it' is. The original may be justified under such an interpretation.

We may be tempted to write (note: each boolean term may be a call with many complex arguments)

    if (A && B && C && D && E)
	...

when it is in fact logically is more like this:

    /* does not make sense to attempt C when A && B does not hold */
    if (A && B) {
    	if (C && D && E)
        	...
    }

But it becomes a judgement call if splitting that into nested two if statements is better for overall readability when the top-level if statement has an else clause. You cannot turn it into

    /* does not make sense to attempt C when A && B does not hold */
    if (A && B) {
    	if (C && D && E)
        	...
    } else {
        ... /* this cannot be what the original's else clause did */
    }

blindly. It would need further restructuring. I think the code inside the refs.c is a result of making such judgement calls.

Quoted 6 lines
Quoted 1 line

By the way, aren't we leaking ce when we are merely pretending?

Yes we are, that's how I found this spot. (coverity pointed out ce was leaking, so I was refactoring to actually make it easier to fix it, and then heavily reordered the patch series afterwards. That spot was forgotten apparently.

I dropped 2/15 and expect the real fix in the future; no rush, though.

#15 lore raw

Re: [PATCH 03/15] read-cache: free cache entry in add_to_index in case of early return

In reply to Stefan Beller
Quoted 18 lines · Stefan Beller
Quoted 9 lines · Junio C Hamano
Quoted 1 line · Stefan Beller

This frees ce would be leaking in the error path.

At this point ce is not yet added to the index, so it is clear it is safe to free it---otherwise we will leak it. Good.

Quoted 1 line

Additionally a free is moved towards the return.

Quoted 7 lines

I am on the fence on this one between two schools and do not have a strong preference. One school is to free as soon as you know you do not need it, which is a valid stance to take. Another is, as you did, not to care about the minimum necessary lifetime of the storage and free them all at the end, which is also valid. Technically, the former could be more performant while the latter is easier on the eyes.

Quoted 5 lines

I only recall to have seen the latter school so far, which is why I made the change in the first place assuming the school of freeing ASAP has no strong supporters inside the git community.

I can resend the patch dropping the reordering, if you prefer.

I already said that I do not have a preference ;-)

Will queue 3/15 as-is, drop 2/15 and wait on 1/15.

#29 lore raw

Re: [PATCH 09/15] http: release the memory of a http pack request as well

In reply to Stefan Beller
Quoted 13 lines · Stefan Beller

The cleanup function is used in 4 places now and it's always safe to free up the memory as well.

---
 http.c | 1 +
 1 file changed, 1 insertion(+)
http.c +0 -0
index 9c825af..4b179f6 100644

http.c void release_http_pack_request(struct http_pack_request *preq) · line 1468

Quoted 7 lines
http.c +1 -0
@@ -1462,6 +1462,7 @@ void release_http_pack_request(struct http_pack_request *preq)
 	}
 	preq->slot = NULL;
 	free(preq->url);
+	free(preq);
 }
 
 int finish_http_pack_request(struct http_pack_request *preq)

Freeing of preq in all the callers of this one looks sensible, except for the one in finish_request() of http-push.c that pulls an preq instance out of request->userData.

Can somebody help me follow the dataflow to convince me that this is not leading to double-free in start_fetch_packed()?

Thanks.

#3 lore raw

Re: [PATCH 01/15] read-cache: fix memleak

In reply to Junio C Hamano
Quoted 30 lines · Junio C Hamano
Quoted 15 lines · Stefan Beller

ce is allocated in make_cache_entry and should be freed if it is not used any more. refresh_cache_entry as a wrapper around refresh_cache_ent will either return ce or a new updated cache entry which is allocated to new memory. In that case we need to free ce ourselfs.

---
 read-cache.c | 2 ++
 1 file changed, 2 insertions(+)
read-cache.c +0 -0
index 8d71860..f72ea9f 100644

read-cache.c struct cache_entry *make_cache_entry(unsigned int mode, · line 754

Quoted 8 lines
read-cache.c +2 -0
@@ -747,6 +747,8 @@ struct cache_entry *make_cache_entry(unsigned int mode,
              free(ce);
              return NULL;
      } else {
+             if (ret != ce)
+                     free(ce);
              return ret;
      }
 }

Good, I vaguely recall that we did something similar in another codepath that forgot the fact that refresh_cache_entry() may make the incoming ce unnecessary.

Quoted 8 lines

As the rule is "if ret is different from ce, then ce must be freed" in this codepath, I wonder if this is easier to read:

        ret = refresh_cache_entry(ce, ...);
        if (ret != ce)
                free(ce);
        return ret;

Indeed it is. Thanks for pointing out!

I'll send this as part of another series
applying on top of sb/leaks soon.

#4 lore raw

PATCH 1/2 read-cache: fix memleak

ce is allocated in make_cache_entry and should be freed if it is not used any more. refresh_cache_entry as a wrapper around refresh_cache_ent will either return ce or a new updated cache entry which is allocated to new memory. In that case we need to free ce ourselfs.

---
 read-cache.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
read-cache.c +2 -5
index 60abec6..a102565 100644
@@ -746,12 +746,9 @@ struct cache_entry *make_cache_entry(unsigned int mode,
 	ce->ce_mode = create_ce_mode(mode);
 
 	ret = refresh_cache_entry(ce, refresh_options);
-	if (!ret) {
+	if (ret != ce)
 		free(ce);
-		return NULL;
-	} else {
-		return ret;
-	}
+	return ret;
 }
 
 int ce_same_name(const struct cache_entry *a, const struct cache_entry *b)
Signature · 1 line
2.3.0.81.gc37f363
#5 lore raw

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

---

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!

 read-cache.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
read-cache.c +8 -4
index a102565..f837212 100644
@@ -706,10 +706,14 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,
 		    !hashcmp(alias->sha1, ce->sha1) &&
 		    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);
+	if (!pretend) {
+		if (add_index_entry(istate, ce, add_option)) {
+			free(ce);
+			return error("unable to add %s to index",path);
+		}
+	} else {
+		free(ce);
+	}
 	if (verbose && !was_same)
 		printf("add '%s'\n", path);
 	return 0;
Signature · 1 line
2.3.0.81.gc37f363
#6 lore raw

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

Quoted 6 lines · Stefan Beller

---

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 <[email protected]>
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 ;-)

---
 read-cache.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
read-cache.c +5 -3
index 60abec6..5b922fd5 100644
@@ -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;
Signature · 1 line
2.3.3-454-g85aa98f
#7 lore raw

Re: [PATCH 1/2] read-cache: fix memleak

In reply to Stefan Beller
Quoted 4 lines · Stefan Beller

ce is allocated in make_cache_entry and should be freed if it is not used any more. refresh_cache_entry as a wrapper around refresh_cache_ent will either return ce or a new updated cache entry which is allocated to new memory. In that case we need to free ce ourselfs.

Thanks. I started "commit --amend" to do s/ourselfs/ourselves/, but ended up tweaking the description a bit more.

-- .8 --
From: Stefan Beller <[email protected]>
Date: Mon, 23 Mar 2015 10:57:11 -0700
Subject: [PATCH] read-cache: fix memleak

ce is allocated in make_cache_entry and should be freed if it is not used any more. refresh_cache_entry as a wrapper around refresh_cache_ent will either return

- the ce given as the parameter, when it was up-to-date;
- a new updated cache entry which is allocated to new memory; or
- a NULL when refreshing failed.

In the latter two cases, the original cache-entry ce is not used and needs to be freed. The rule can be expressed as "if the return value from refresh is different from the original ce, ce is no longer used."

---
 read-cache.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
read-cache.c +2 -5
index 5b922fd5..0052b72 100644
@@ -748,12 +748,9 @@ struct cache_entry *make_cache_entry(unsigned int mode,
 	ce->ce_mode = create_ce_mode(mode);
 
 	ret = refresh_cache_entry(ce, refresh_options);
-	if (!ret) {
+	if (ret != ce)
 		free(ce);
-		return NULL;
-	} else {
-		return ret;
-	}
+	return ret;
 }
 
 int ce_same_name(const struct cache_entry *a, const struct cache_entry *b)
Signature · 1 line
2.3.3-454-g85aa98f
#18 lore raw

PATCH 1/1 update-index: Don't copy memory around

In reply to Junio C Hamano

---

Quoted 28 lines · Junio C Hamano
Quoted 13 lines · Stefan Beller

old is not used outside the loop and would get lost once we reach the goto.

---
 builtin/update-index.c | 1 +

 1 file changed, 1 insertion(+)
builtin/update-index.c +0 -0
index 5878986..6271b54 100644

builtin/update-index.c static int do_reupdate(int ac, const char **av, · line 587

Quoted 4 lines
builtin/update-index.c +1 -0
@@ -584,6 +584,7 @@ static int do_reupdate(int ac, const char **av,
               path = xstrdup(ce->name);
               update_one(path);
               free(path);
+             free(old);

The change looks trivially correct.

A tangent; I wonder if we want to make path a strbuf that is declared in the function scope, reset (not released) at each iteration of the loop and released after the loop; keep allocating and freeing a small piece of string all the time feels somewhat wasteful.

Quoted 3 lines

Also, we might want to add to the "Be careful" comment to describe why this cannot just be a call to "update_one(ce->name)" that does not use "path".

Indeed I am rather wondering why we need to pass in a copy to update_one instead of ce->name. I was reading update_one and looking for why, but eventually noticed update_one accepts a 'const char *', so it's not changed in there. Digging down the into update_one also doesn't make it obvious to me.

I found (5699d17ee094, 2013-11-14, read-cache.c: fix memory leaks caused by removed cache entries), which adds this duplication, though I do not understand why. This passes the test suite, so I wonder if this patch would be a subtle bug now.

 builtin/update-index.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
builtin/update-index.c +1 -4
index 6271b54..5857405 100644
@@ -564,7 +564,6 @@ static int do_reupdate(int ac, const char **av,
 		const struct cache_entry *ce = active_cache[pos];
 		struct cache_entry *old = NULL;
 		int save_nr;
-		char *path;
 
 		if (ce_stage(ce) || !ce_path_match(ce, &pathspec, NULL))
 			continue;
@@ -581,9 +580,7 @@ static int do_reupdate(int ac, const char **av,
 		 * or worse yet 'allow_replace', active_nr may decrease.
 		 */
 		save_nr = active_nr;
-		path = xstrdup(ce->name);
-		update_one(path);
-		free(path);
+		update_one(ce->name);
 		free(old);
 		if (save_nr != active_nr)
 			goto redo;
Signature · 1 line
2.3.0.81.gc37f363
#19 lore raw

Re: [PATCH] update-index: Don't copy memory around

Quoted 3 lines · Stefan Beller

... though I do not understand
why. This passes the test suite, so I wonder if this patch would be a subtle bug now.

I recall the last time I traced the code I noticed that the ce can get passed to a codepath that causes its removal from update_one(), and then the pathname itself (which used to be ce->name but remember that ce is invalid already at that point!) is still used after ce is removed, which was the reason why you would introduce a bug if you stop copying the path. You may have to follow the code again a bit closer to make sure but I am reasonably sure that was the reason.

#22 lore raw

PATCH 1/1 builtin/apply.c: fix a memleak (Fixup, squashable)

In reply to Junio C Hamano

---

Quoted 5 lines

By the way, aren't the following also leaking here?

 - the strbuf newlines that starts out as "size"
 - line[] arrays of preimage and postimage

yes :(

I think a viable replacement may be below.
That way the returned value of the function is not -1, but 1. We're using the return value only as a boolean, so it doesn't really matter.

 builtin/apply.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
builtin/apply.c +3 -2
index e152c4d..0769b09 100644
@@ -2776,8 +2776,8 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
 		default:
 			if (apply_verbosely)
 				error(_("invalid start of line: '%c'"), first);
-			free(oldlines);
-			return -1;
+			applied_pos = -1;
+			goto out;
 		}
 		if (added_blank_line) {
 			if (!new_blank_lines_at_end)
@@ -2916,6 +2916,7 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
 			      (int)(old - oldlines), oldlines);
 	}
 
+out:
 	free(oldlines);
 	strbuf_release(&newlines);
 	free(preimage.line_allocated);
Signature · 1 line
2.3.0.81.gc37f363
#23 lore raw

Re: [PATCH] builtin/apply.c: fix a memleak (Fixup, squashable)

Quoted 5 lines · Stefan Beller

---

Quoted 2 lines

By the way, aren't the following also leaking here?

Quoted 7 lines
Quoted 3 lines

 - the strbuf newlines that starts out as "size"
 - line[] arrays of preimage and postimage

yes :(

I think a viable replacement may be below.

Yeah, that looks a lot more sensible and straight-forward.

Thanks.

Quoted 30 lines

That way the returned value of the function is not -1, but 1. We're using the return value only as a boolean, so it doesn't really matter.

 builtin/apply.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
builtin/apply.c +3 -2
index e152c4d..0769b09 100644
@@ -2776,8 +2776,8 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
 		default:
 			if (apply_verbosely)
 				error(_("invalid start of line: '%c'"), first);
-			free(oldlines);
-			return -1;
+			applied_pos = -1;
+			goto out;
 		}
 		if (added_blank_line) {
 			if (!new_blank_lines_at_end)
@@ -2916,6 +2916,7 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,
 			      (int)(old - oldlines), oldlines);
 	}
 
+out:
 	free(oldlines);
 	strbuf_release(&newlines);
 	free(preimage.line_allocated);
#30 lore raw

Re: [PATCH 09/15] http: release the memory of a http pack request as well

In reply to Junio C Hamano
Quoted 21 lines · Junio C Hamano
Quoted 19 lines · Stefan Beller

The cleanup function is used in 4 places now and it's always safe to free up the memory as well.

---
 http.c | 1 +
 1 file changed, 1 insertion(+)
http.c +1 -0
index 9c825af..4b179f6 100644
@@ -1462,6 +1462,7 @@ void release_http_pack_request(struct http_pack_request *preq)
      }
      preq->slot = NULL;
      free(preq->url);
+     free(preq);
 }
 
Quoted 8 lines

http.c void release_http_pack_request(struct http_pack_request *preq) · line 1468

Quoted 1 line
http.c +0 -0
 int finish_http_pack_request(struct http_pack_request *preq)

Freeing of preq in all the callers of this one looks sensible, except for the one in finish_request() of http-push.c that pulls an preq instance out of request->userData.

Can somebody help me follow the dataflow to convince me that this is not leading to double-free in start_fetch_packed()?

I am not sure where you suspect the double free problem.

In start_fetch_packed we have a call to release_http_pack_request 2 times but just in an error-out-and-cleanup case, so either of one cases is called.

In the latter place (http-push.c lines 335-347), we have code like

    request->userData = preq;
    if (!start_active_slot(preq->slot)) {
        release_http_pack_request(preq);
        repo->can_update_info_refs = 0;
        release_request(request);
    }

Do you mean that the release_http_pack_request and release_request may collide as the release_request(request); has a pointer to preq via its userData field?

Well there is hope, as release_request only touches

    free(request->url);
    free(request);

and not the userData pointer.

I am a bit puzzled what you're trying to hint at.

Quoted 3 lines

Thanks.

#31 lore raw

Re: [PATCH 09/15] http: release the memory of a http pack request as well

Quoted 5 lines · Stefan Beller

Well there is hope, as release_request only touches

    free(request->url);
    free(request);

and not the userData pointer.

OK.

Quoted 1 line

I am a bit puzzled what you're trying to hint at.

The caller does this:

        static void start_fetch_packed(struct transfer_request *request)
        {
                ...
                preq = new_http_pack_request(target, repo->url);
                ...
                preq->slot->callback_func = process_response;
                preq->slot->callback_data = request;
                request->slot = preq->slot;
                request->userData = preq;

                /* Try to get the request started, abort the request on error */
                request->state = RUN_FETCH_PACKED;
                if (!start_active_slot(preq->slot)) {
                        fprintf(stderr, "Unable to start GET request\n");
                        release_http_pack_request(preq);
                        repo->can_update_info_refs = 0;
                        release_request(request);
                }
        }

and start_active_slot() actually not just "starts" but calls curl_multi_perform() to do things, like calling process_response(), which has calls to release_{,http_pack_}request(). I didn't see those releases and the releases we see in the above (i.e. when !start) will not run at the same time (but I see it now ;-))

In short, not hinting at anything. I was genuinely having a hard time following the codeflow.

#34 lore raw

Re: [PATCH 10/15] commit.c: fix a memory leak

In reply to Junio C Hamano
Quoted 9 lines · Junio C Hamano

A further tangent (Duy Cc'ed for this point). We might want to rethink the interface to ce_path_match() and report_path_error() so that we do not have to do a separate allocation of "has this pathspec been used?" array. This was a remnant from the olden days back when pathspec were mere "const char **" where we did not have any room to add per-item bit---these days pathspec is repreasented as an array of "struct pathspec" and we can afford to add a bit to the structure---which will make this kind of leak much less likely to happen.

I just want to say "noted" (and therefore in my backlog). But no promise that it will happen any time soon. Low hanging fruit, perhaps some people may be interested..

Signature · 1 line
Duy
#35 lore raw

Re* [PATCH 10/15] commit.c: fix a memory leak

Quoted 11 lines · Duy Nguyen
Quoted 9 lines · Junio C Hamano

A further tangent (Duy Cc'ed for this point). We might want to rethink the interface to ce_path_match() and report_path_error() so that we do not have to do a separate allocation of "has this pathspec been used?" array. This was a remnant from the olden days back when pathspec were mere "const char **" where we did not have any room to add per-item bit---these days pathspec is repreasented as an array of "struct pathspec" and we can afford to add a bit to the structure---which will make this kind of leak much less likely to happen.

Quoted 3 lines

I just want to say "noted" (and therefore in my backlog). But no promise that it will happen any time soon. Low hanging fruit, perhaps some people may be interested..

OK, the other one I just did so that we won't forget. Otherwise we will leave too many loose ends untied.

-- >8 --
From: Junio C Hamano <[email protected]>
Date: Tue, 24 Mar 2015 14:12:10 -0700
Subject: [PATCH] report_path_error(): move to dir.c

The expected call sequence is for the caller to use match_pathspec() repeatedly on a set of pathspecs, accumulating the "hits" in a separate array, and then call this function to diagnose a pathspec that never matched anything, as that can indicate a typo from the command line, e.g. "git commit Maekfile".

Many builtin commands use this function from builtin/ls-files.c, which is not a very healthy arrangement. ls-files might have been the first command to feel the need for such a helper, but the need is shared by everybody who uses the "match and then report" pattern.

Move it to dir.c where match_pathspec() is defined.

---
 builtin/ls-files.c | 43 -------------------------------------------
 cache.h            |  1 -
 dir.c              | 43 +++++++++++++++++++++++++++++++++++++++++++
 dir.h              |  1 +
 4 files changed, 44 insertions(+), 44 deletions(-)
builtin/ls-files.c +0 -43
index 47c3880..47d70b2 100644
@@ -354,49 +354,6 @@ void overlay_tree_on_cache(const char *tree_name, const char *prefix)
 	}
 }
 
-int report_path_error(const char *ps_matched,
-		      const struct pathspec *pathspec,
-		      const char *prefix)
-{
-	/*
-	 * Make sure all pathspec matched; otherwise it is an error.
-	 */
-	struct strbuf sb = STRBUF_INIT;
-	int num, errors = 0;
-	for (num = 0; num < pathspec->nr; num++) {
-		int other, found_dup;
-
-		if (ps_matched[num])
-			continue;
-		/*
-		 * The caller might have fed identical pathspec
-		 * twice.  Do not barf on such a mistake.
-		 * FIXME: parse_pathspec should have eliminated
-		 * duplicate pathspec.
-		 */
-		for (found_dup = other = 0;
-		     !found_dup && other < pathspec->nr;
-		     other++) {
-			if (other == num || !ps_matched[other])
-				continue;
-			if (!strcmp(pathspec->items[other].original,
-				    pathspec->items[num].original))
-				/*
-				 * Ok, we have a match already.
-				 */
-				found_dup = 1;
-		}
-		if (found_dup)
-			continue;
-
-		error("pathspec '%s' did not match any file(s) known to git.",
-		      pathspec->items[num].original);
-		errors++;
-	}
-	strbuf_release(&sb);
-	return errors;
-}
-
 static const char * const ls_files_usage[] = {
 	N_("git ls-files [options] [<file>...]"),
 	NULL
cache.h +0 -1
index f23fdbe..8ec0b65 100644
@@ -1411,7 +1411,6 @@ extern int ws_blank_line(const char *line, int len, unsigned ws_rule);
 #define ws_tab_width(rule)     ((rule) & WS_TAB_WIDTH_MASK)
 
 /* ls-files */
-int report_path_error(const char *ps_matched, const struct pathspec *pathspec, const char *prefix);
 void overlay_tree_on_cache(const char *tree_name, const char *prefix);
 
 char *alias_lookup(const char *alias);
dir.c +43 -0

49 lines not on this page · raw message

dir.h +1 -0
index 55e5345..ed336ad 100644
@@ -135,6 +135,7 @@ extern char *common_prefix(const struct pathspec *pathspec);
 extern int match_pathspec(const struct pathspec *pathspec,
 			  const char *name, int namelen,
 			  int prefix, char *seen, int is_dir);
+extern int report_path_error(const char *ps_matched, const struct pathspec *pathspec, const char *prefix);
 extern int within_depth(const char *name, int namelen, int depth, int max_depth);
 
 extern int fill_directory(struct dir_struct *dir, const struct pathspec *pathspec);
Signature · 1 line
2.3.4-449-gebec173