Skip to contentSkip to conversation

Some cleanups

PATCH Superseded Stefan Beller 5 participants 20 messages Jun 15, 2016

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover Some cleanups

v2:
Thanks Eric, Jeff, Junio for discussion, I picked up all hints and here is a v2.

* drop the overallocation patches (1&2)
* use git_config_get_string instead of its _const equivalent, such that we don't need a cast when freeing in git_config_get_notes_strategy
* Use strbuf_list_free instead of cooking our own.
* have a dedicated error exit path in bundle.c, create_bundle

v1:
One of my first patches to Git were cleanup patches, and I fell back to my old pattern here, while thinking on how to write better commit messages for the submodule bugfixes I currently have in flight.

Just some one liners to not leak memory or file descriptors.

They are bundled as a series, but no patch relies on any predessor.

This applies on v2.8.0.

Thanks,
Stefan

Stefan Beller (4):
notes: don't leak memory in git_config_get_notes_strategy
abbrev_sha1_in_line: don't leak memory
bundle: don't leak an fd in case of early return credential-cache, send_request: close fd when done

 builtin/notes.c    |  5 +++--
 bundle.c           | 23 +++++++++++++++++------
 credential-cache.c |  1 +
 wt-status.c        |  4 +---
 4 files changed, 22 insertions(+), 11 deletions(-)
Signature · 1 line
2.8.0.2.gb331331
#1 lore raw

PATCH v2 1/4 notes: don't leak memory in git_config_get_notes_strategy

value is just a temporary scratchpad, so we need to make sure it doesn't leak. It is xstrdup'd in git_config_get_string and parse_notes_merge_strategy just compares the string against predefined values, so no need to keep it around longer. Make value non-const to avoid the cast in the free.

---
 builtin/notes.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
builtin/notes.c +3 -2
index ed6f222..6fd058d 100644
@@ -744,13 +744,14 @@ static int merge_commit(struct notes_merge_options *o)
 static int git_config_get_notes_strategy(const char *key,
 					 enum notes_merge_strategy *strategy)
 {
-	const char *value;
+	char *value;
 
-	if (git_config_get_string_const(key, &value))
+	if (git_config_get_string(key, &value))
 		return 1;
 	if (parse_notes_merge_strategy(value, strategy))
 		git_die_config(key, "unknown notes merge strategy %s", value);
 
+	free(value);
 	return 0;
 }
 
Signature · 1 line
2.8.0.2.gb331331
#2 lore raw

Re: [PATCHv2 1/4] notes: don't leak memory in git_config_get_notes_strategy

Quoted 15 lines · Stefan Beller

value is just a temporary scratchpad, so we need to make sure it doesn't leak. It is xstrdup'd in git_config_get_string and parse_notes_merge_strategy just compares the string against predefined values, so no need to keep it around longer. Make value non-const to avoid the cast in the free.

---

builtin/notes.c +1 -1
@@ -744,13 +744,14 @@ static int merge_commit(struct notes_merge_options *o)
 static int git_config_get_notes_strategy(const char *key,
                                         enum notes_merge_strategy *strategy)
 {
-       const char *value;
+       char *value;

builtin/notes.c static int merge_commit(struct notes_merge_options *o) · line 756

Quoted 10 lines
builtin/notes.c +2 -1
 
-       if (git_config_get_string_const(key, &value))
+       if (git_config_get_string(key, &value))
                return 1;
        if (parse_notes_merge_strategy(value, strategy))
                git_die_config(key, "unknown notes merge strategy %s", value);
 
+       free(value);
        return 0;
 }

Hmm, I thought Peff's suggestion of using git_config_get_value() was accepted as superior since it avoids the allocation altogether, thus no need for free() and no leak.

#3 lore raw

Re: [PATCHv2 1/4] notes: don't leak memory in git_config_get_notes_strategy

Quoted 14 lines · Eric Sunshine
Quoted 14 lines
builtin/notes.c +2 -2
@@ -744,13 +744,14 @@ static int merge_commit(struct notes_merge_options *o)
 static int git_config_get_notes_strategy(const char *key,
                                         enum notes_merge_strategy *strategy)
 {
-       const char *value;
+       char *value;
 
-       if (git_config_get_string_const(key, &value))
+       if (git_config_get_string(key, &value))
                return 1;
        if (parse_notes_merge_strategy(value, strategy))
                git_die_config(key, "unknown notes merge strategy %s", value);
 
Quoted 7 lines

builtin/notes.c static int merge_commit(struct notes_merge_options *o) · line 756

Quoted 3 lines
builtin/notes.c +1 -0
+       free(value);
        return 0;
 }

Hmm, I thought Peff's suggestion of using git_config_get_value() was accepted as superior since it avoids the allocation altogether, thus no need for free() and no leak.

I agree that this caller can avoid taking ownership of value by using git_config_get_value() and that would be a cleaner solution here.

This is a tangent, but am I the only one who finds that the naming of functions in config-get API is confusing? Just wondering if we should rename the ones that keeps the memory ownership to the config subsystem with s/get/peek/ or something.

#4 lore raw

Re: [PATCHv2 1/4] notes: don't leak memory in git_config_get_notes_strategy

Quoted 4 lines · Junio C Hamano

This is a tangent, but am I the only one who finds that the naming of functions in config-get API is confusing? Just wondering if we should rename the ones that keeps the memory ownership to the config subsystem with s/get/peek/ or something.

You are definitely not the only one.

I think the get/peek thing would help, but I also get confused between git_config_string() and git_config_get_string().

-Peff

#5 lore raw

Re: [PATCHv2 1/4] notes: don't leak memory in git_config_get_notes_strategy

Quoted 11 lines · Jeff King
Quoted 4 lines · Junio C Hamano

This is a tangent, but am I the only one who finds that the naming of functions in config-get API is confusing? Just wondering if we should rename the ones that keeps the memory ownership to the config subsystem with s/get/peek/ or something.

You are definitely not the only one.

I think the get/peek thing would help, but I also get confused between git_config_string() and git_config_get_string().

I actually do not have problems with the ones with names that do not have "get" in.

git_config_$type() is always a helper function that are designed to be called by config callback to parse (and complain) a <var, val> pair that is expected to be of $type, and git_$family_config() is always a helper function that are designed to be used as the callback of git_config() to handle the configuration related to the named command family.

#6 lore raw

Re: [PATCHv2 1/4] notes: don't leak memory in git_config_get_notes_strategy

In reply to Junio C Hamano
Quoted 24 lines · Junio C Hamano
Quoted 14 lines · Eric Sunshine
Quoted 14 lines
builtin/notes.c +2 -2
@@ -744,13 +744,14 @@ static int merge_commit(struct notes_merge_options *o)
 static int git_config_get_notes_strategy(const char *key,
                                         enum notes_merge_strategy *strategy)
 {
-       const char *value;
+       char *value;
 
-       if (git_config_get_string_const(key, &value))
+       if (git_config_get_string(key, &value))
                return 1;
        if (parse_notes_merge_strategy(value, strategy))
                git_die_config(key, "unknown notes merge strategy %s", value);
 
Quoted 7 lines

builtin/notes.c static int merge_commit(struct notes_merge_options *o) · line 756

Quoted 3 lines
builtin/notes.c +1 -0
+       free(value);
        return 0;
 }

Hmm, I thought Peff's suggestion of using git_config_get_value() was accepted as superior since it avoids the allocation altogether, thus no need for free() and no leak.

Quoted 9 lines

I agree that this caller can avoid taking ownership of value by using git_config_get_value() and that would be a cleaner solution here.

This is a tangent, but am I the only one who finds that the naming of functions in config-get API is confusing? Just wondering if we should rename the ones that keeps the memory ownership to the config subsystem with s/get/peek/ or something.

I demonstrated the confusion and disability to read and distinguish between those names clearly already.

So I'd strongly favor a better naming for functions in config.c

#7 lore raw

PATCH v2 2/4 abbrev_sha1_in_line: don't leak memory

In reply to Stefan Beller

split is of type struct strbuf **, which we have a dedicated free function for, which takes care of freeing all related memory.

---
 wt-status.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
wt-status.c +1 -3
index ef74864..1ea2ebe 100644
@@ -1063,9 +1063,7 @@ static void abbrev_sha1_in_line(struct strbuf *line)
 				strbuf_addf(line, "%s", split[i]->buf);
 		}
 	}
-	for (i = 0; split[i]; i++)
-		strbuf_release(split[i]);
-
+	strbuf_list_free(split);
 }
 
 static void read_rebase_todolist(const char *fname, struct string_list *lines)
Signature · 1 line
2.8.0.2.gb331331
#8 lore raw

Re: [PATCHv2 2/4] abbrev_sha1_in_line: don't leak memory

Quoted 2 lines · Stefan Beller

split is of type struct strbuf **, which we have a dedicated free function for, which takes care of freeing all related memory.

I think it's important to explain that 'split' and each split[] element were being leaked (despite the existing strbuf_release()) as justification for why this change is beneficial.

Quoted 19 lines
---
 wt-status.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
wt-status.c +1 -3
index ef74864..1ea2ebe 100644
@@ -1063,9 +1063,7 @@ static void abbrev_sha1_in_line(struct strbuf *line)
                                strbuf_addf(line, "%s", split[i]->buf);
                }
        }
-       for (i = 0; split[i]; i++)
-               strbuf_release(split[i]);
-
+       strbuf_list_free(split);
 }
#9 lore raw

PATCH v2 3/4 bundle: don't leak an fd in case of early return

In reply to Stefan Beller

In successful operation write_pack_data will close the bundle_fd, but when we exit early, we need to take care of the file descriptor as well as the lock file ourselves. The lock file may be deleted at the end of running the program, but we are in library code, so we should not rely on that.

---
 bundle.c | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)
bundle.c +17 -6
index 506ac49..fbc8593 100644
@@ -407,6 +407,7 @@ int create_bundle(struct bundle_header *header, const char *path,
 	int bundle_to_stdout;
 	int ref_count = 0;
 	struct rev_info revs;
+	int ret = -1;
 
 	bundle_to_stdout = !strcmp(path, "-");
 	if (bundle_to_stdout)
@@ -435,30 +436,40 @@ int create_bundle(struct bundle_header *header, const char *path,
 
 	/* write prerequisites */
 	if (compute_and_write_prerequisites(bundle_fd, &revs, argc, argv))
-		return -1;
+		goto err;
 
 	argc = setup_revisions(argc, argv, &revs, NULL);
 
-	if (argc > 1)
-		return error(_("unrecognized argument: %s"), argv[1]);
+	if (argc > 1) {
+		ret = error(_("unrecognized argument: %s"), argv[1]);
+		goto err;
+	}
 
 	object_array_remove_duplicates(&revs.pending);
 
 	ref_count = write_bundle_refs(bundle_fd, &revs);
 	if (!ref_count)
 		die(_("Refusing to create empty bundle."));
-	else if (ref_count < 0)
-		return -1;
+	else if (ref_count < 0) {
+		if (!bundle_to_stdout)
+			close(bundle_fd);
+		goto err;
+	}
 
 	/* write pack */
 	if (write_pack_data(bundle_fd, &revs))
-		return -1;
+		goto err;
 
 	if (!bundle_to_stdout) {
 		if (commit_lock_file(&lock))
 			die_errno(_("cannot create '%s'"), path);
 	}
 	return 0;
+err:
+	if (!bundle_to_stdout)
+		close(bundle_fd);
+	rollback_lock_file(&lock);
+	return ret;
 }
 
 int unbundle(struct bundle_header *header, int bundle_fd, int flags)
Signature · 1 line
2.8.0.2.gb331331
#10 lore raw

Re: [PATCHv2 3/4] bundle: don't leak an fd in case of early return

bundle.c int create_bundle(struct bundle_header *header, const char *path, · line 410

Quoted 9 lines · Stefan Beller
bundle.c +1 -0
index 506ac49..fbc8593 100644
@@ -407,6 +407,7 @@ int create_bundle(struct bundle_header *header, const char *path,
 	int bundle_to_stdout;
 	int ref_count = 0;
 	struct rev_info revs;
+	int ret = -1;

A minor nit, but I don't think we ever put anything but "-1" in this variable. It could go away and we can just "return -1" in the "err" path.

-Peff

#11 lore raw

Re: [PATCHv2 3/4] bundle: don't leak an fd in case of early return

In reply to Stefan Beller
Quoted 26 lines · Stefan Beller

In successful operation write_pack_data will close the bundle_fd, but when we exit early, we need to take care of the file descriptor as well as the lock file ourselves. The lock file may be deleted at the end of running the program, but we are in library code, so we should not rely on that.

---

bundle.c +5 -3
@@ -435,30 +436,40 @@ int create_bundle(struct bundle_header *header, const char *path,
 
        /* write prerequisites */
        if (compute_and_write_prerequisites(bundle_fd, &revs, argc, argv))
-               return -1;
+               goto err;
 
        argc = setup_revisions(argc, argv, &revs, NULL);
 
-       if (argc > 1)
-               return error(_("unrecognized argument: %s"), argv[1]);
+       if (argc > 1) {
+               ret = error(_("unrecognized argument: %s"), argv[1]);
+               goto err;
+       }
 

bundle.c int create_bundle(struct bundle_header *header, const char *path, · line 455

Quoted 10 lines
bundle.c +3 -2
        object_array_remove_duplicates(&revs.pending);
 
        ref_count = write_bundle_refs(bundle_fd, &revs);
        if (!ref_count)
                die(_("Refusing to create empty bundle."));
-       else if (ref_count < 0)
-               return -1;
+       else if (ref_count < 0) {
+               if (!bundle_to_stdout)
+                       close(bundle_fd);

Why is this close() here considering that it gets closed by the 'err' path?

Quoted 19 lines
bundle.c +8 -1
+               goto err;
+       }
 
        /* write pack */
        if (write_pack_data(bundle_fd, &revs))
-               return -1;
+               goto err;
 
        if (!bundle_to_stdout) {
                if (commit_lock_file(&lock))
                        die_errno(_("cannot create '%s'"), path);
        }
        return 0;
+err:
+       if (!bundle_to_stdout)
+               close(bundle_fd);
+       rollback_lock_file(&lock);
+       return ret;
 }
#12 lore raw

Re: [PATCHv2 3/4] bundle: don't leak an fd in case of early return

Quoted 7 lines · Eric Sunshine

bundle.c int create_bundle(struct bundle_header *header, const char *path, · line 455

Quoted 5 lines
bundle.c +3 -2
-       else if (ref_count < 0)
-               return -1;
+       else if (ref_count < 0) {
+               if (!bundle_to_stdout)
+                       close(bundle_fd);

Why is this close() here considering that it gets closed by the 'err' path?

Thanks for pointing out; fixed in a reroll.

#13 lore raw

Re: [PATCHv2 3/4] bundle: don't leak an fd in case of early return

In reply to Stefan Beller

From: "Stefan Beller" <[email protected]>

Quoted 8 lines

In successful operation write_pack_data will close the bundle_fd, but when we exit early, we need to take care of the file descriptor as well as the lock file ourselves. The lock file may be deleted at the end of running the program, but we are in library code, so we should not rely on that.

Has this been tested on Windows? I had a similar problem very recently
https://groups.google.com/forum/#!msg/git-for-windows/6LPxf9xZKhI/-s7XD18yCwAJ
where a bad rev-list-arg would cause the bundle create to die, and, on windows, leave the incomplete bundle file locked.

dscho suggested one possible solution for that, but I haven't had any time to try any patches.

Quoted 73 lines

---
bundle.c | 23 +++++++++++++++++------

1 file changed, 17 insertions(+), 6 deletions(-)
bundle.c +1 -0
index 506ac49..fbc8593 100644
@@ -407,6 +407,7 @@ int create_bundle(struct bundle_header *header, const 
 char *path,
 int bundle_to_stdout;
 int ref_count = 0;
 struct rev_info revs;
+ int ret = -1;
 
 bundle_to_stdout = !strcmp(path, "-");

if (bundle_to_stdout)

bundle.c +16 -6
@@ -435,30 +436,40 @@ int create_bundle(struct bundle_header *header, 
 const char *path,
 
 /* write prerequisites */
 if (compute_and_write_prerequisites(bundle_fd, &revs, argc, argv))
- return -1;
+ goto err;
 
 argc = setup_revisions(argc, argv, &revs, NULL);
 
- if (argc > 1)
- return error(_("unrecognized argument: %s"), argv[1]);
+ if (argc > 1) {
+ ret = error(_("unrecognized argument: %s"), argv[1]);
+ goto err;
+ }
 
 object_array_remove_duplicates(&revs.pending);
 
 ref_count = write_bundle_refs(bundle_fd, &revs);
 if (!ref_count)
 die(_("Refusing to create empty bundle."));
- else if (ref_count < 0)
- return -1;
+ else if (ref_count < 0) {
+ if (!bundle_to_stdout)
+ close(bundle_fd);
+ goto err;
+ }
 
 /* write pack */
 if (write_pack_data(bundle_fd, &revs))
- return -1;
+ goto err;
 
 if (!bundle_to_stdout) {
 if (commit_lock_file(&lock))
 die_errno(_("cannot create '%s'"), path);
 }
 return 0;
+err:
+ if (!bundle_to_stdout)
+ close(bundle_fd);
+ rollback_lock_file(&lock);
+ return ret;
 }
 

int unbundle(struct bundle_header *header, int bundle_fd, int flags)

Signature · 6 lines
2.8.0.2.gb331331

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#14 lore raw

Re: [PATCHv2 3/4] bundle: don't leak an fd in case of early return

Quoted 12 lines · Philip Oakley

From: "Stefan Beller" <[email protected]>

Quoted 9 lines

In successful operation write_pack_data will close the bundle_fd, but when we exit early, we need to take care of the file descriptor as well as the lock file ourselves. The lock file may be deleted at the end of running the program, but we are in library code, so we should not rely on that.

Quoted 7 lines

Has this been tested on Windows? I had a similar problem very recently
https://groups.google.com/forum/#!msg/git-for-windows/6LPxf9xZKhI/-s7XD18yCwAJ
where a bad rev-list-arg would cause the bundle create to die, and, on windows, leave the incomplete bundle file locked.

dscho suggested one possible solution for that, but I haven't had any time to try any patches.

I think with Jeffs suggestion to only rollback the lock in case of (!bundle_to_stdout)
we're not making it worse. I do not have a Windows machine at hand to test it :(

#15 lore raw

Re: [PATCHv2 3/4] bundle: don't leak an fd in case of early return

From: "Stefan Beller" <[email protected]>

Quoted 18 lines · Philip Oakley
Quoted 17 lines

From: "Stefan Beller" <[email protected]>

Quoted 9 lines

In successful operation write_pack_data will close the bundle_fd, but when we exit early, we need to take care of the file descriptor as well as the lock file ourselves. The lock file may be deleted at the end of running the program, but we are in library code, so we should not rely on that.

Has this been tested on Windows? I had a similar problem very recently
https://groups.google.com/forum/#!msg/git-for-windows/6LPxf9xZKhI/-s7XD18yCwAJ
where a bad rev-list-arg would cause the bundle create to die, and, on windows, leave the incomplete bundle file locked.

Quoted 6 lines
Quoted 2 lines

dscho suggested one possible solution for that, but I haven't had any time to try any patches.

I think with Jeffs suggestion to only rollback the lock in case of (!bundle_to_stdout)
we're not making it worse. I do not have a Windows machine at hand to test it :(

Thant's fine. I just wanted to make folks aware of the problem so it doesn't get worse.

It looks like the cause is that the bundle.c:compute_and_write_prerequisites(), which includes parsing the arg-list (and die on bad arg), is called after the creation and locking of the bundle file descriptor and header, thus leaving it hanging on Windows.

--

Philip

#16 lore raw

PATCH v2 4/4 credential-cache, send_request: close fd when done

In reply to Stefan Beller

No need to keep it open any further.

---
 credential-cache.c | 1 +
 1 file changed, 1 insertion(+)
credential-cache.c +1 -0
index f4afdc6..86e21de 100644
@@ -32,6 +32,7 @@ static int send_request(const char *socket, const struct strbuf *out)
 		write_or_die(1, in, r);
 		got_data = 1;
 	}
+	close(fd);
 	return got_data;
 }
 
Signature · 1 line
2.8.0.2.gb331331
#17 lore raw
In reply to Stefan Beller
Quoted 9 lines · Stefan Beller

v2:
Thanks Eric, Jeff, Junio for discussion, I picked up all hints and here is a v2.

* drop the overallocation patches (1&2)
* use git_config_get_string instead of its _const equivalent, such that we don't need a cast when freeing in git_config_get_notes_strategy
* Use strbuf_list_free instead of cooking our own.
* have a dedicated error exit path in bundle.c, create_bundle

I'm OK with all of these as-is, though I did mention a nit in the third one. I also like Junio's rewrite instead of using strbuf_list_free.

-Peff

#18 lore raw
Quoted 6 lines · Jeff King
Quoted 4 lines · Stefan Beller

v2:
Thanks Eric, Jeff, Junio for discussion, I picked up all hints and here is a v2.

Quoted 8 lines
Quoted 5 lines

* drop the overallocation patches (1&2)
* use git_config_get_string instead of its _const equivalent, such that we don't need a cast when freeing in git_config_get_notes_strategy
* Use strbuf_list_free instead of cooking our own.
* have a dedicated error exit path in bundle.c, create_bundle

I'm OK with all of these as-is, though I did mention a nit in the third one. I also like Junio's rewrite instead of using strbuf_list_free.

I'm fine using the rewritten version instead of using strbuf_list_free. :) On the third one, there is one case, where we have

  if (..)
    return error(_(text));

and that is an exit(128); eventually.

I thought it is worth preserving the difference (as it is a faithful bug fix not a
change to make it better or more uniform).

Thanks,
Stefan

Quoted 2 lines

-Peff

#19 lore raw
Quoted 10 lines · Stefan Beller
Quoted 2 lines

I'm OK with all of these as-is, though I did mention a nit in the third one. I also like Junio's rewrite instead of using strbuf_list_free.

I'm fine using the rewritten version instead of using strbuf_list_free. :) On the third one, there is one case, where we have

  if (..)
    return error(_(text));

and that is an exit(128); eventually.

In the caller perhaps, but isn't that equivalent to:

  error(_(text));
  return -1;

?

I think it is OK to make assumptions about error()'s return value; that is what it is there for.

-Peff