All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] blame.c: fix garbled error message
@ 2015-01-10 21:33 Lukas Fleischer
  2015-01-12 19:08 ` Junio C Hamano
  0 siblings, 1 reply; 19+ messages in thread
From: Lukas Fleischer @ 2015-01-10 21:33 UTC (permalink / raw)
  To: git

The helper functions prepare_final() and prepare_initial() return a
pointer to a string that is a member of an object in the revs->pending
array. This array is later rebuilt when running prepare_revision_walk()
which potentially transforms the pointer target into a bogus string. Fix
this by maintaining a copy of the original string.

Signed-off-by: Lukas Fleischer <git@cryptocrack.de>
---
The bug manifests when running `git blame HEAD^ -- nonexistent.file`.

Note that I could have reduced code churn a little by moving the
xstrdup() invocations to the call sites. However, I think that the
return value of these functions should not depend on the consistency of
a volatile data structure. On the other hand, you might also argue that
there currently are only two call sites and that the functions are
marked static, so if you prefer the less intrusive version, please let
me know.

 builtin/blame.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/builtin/blame.c b/builtin/blame.c
index 303e217..34d6f4f 100644
--- a/builtin/blame.c
+++ b/builtin/blame.c
@@ -2390,7 +2390,7 @@ static struct commit *fake_working_tree_commit(struct diff_options *opt,
 	return commit;
 }
 
-static const char *prepare_final(struct scoreboard *sb)
+static char *prepare_final(struct scoreboard *sb)
 {
 	int i;
 	const char *final_commit_name = NULL;
@@ -2415,10 +2415,10 @@ static const char *prepare_final(struct scoreboard *sb)
 		sb->final = (struct commit *) obj;
 		final_commit_name = revs->pending.objects[i].name;
 	}
-	return final_commit_name;
+	return xstrdup(final_commit_name);
 }
 
-static const char *prepare_initial(struct scoreboard *sb)
+static char *prepare_initial(struct scoreboard *sb)
 {
 	int i;
 	const char *final_commit_name = NULL;
@@ -2445,7 +2445,7 @@ static const char *prepare_initial(struct scoreboard *sb)
 	}
 	if (!final_commit_name)
 		die("No commit to dig down to?");
-	return final_commit_name;
+	return xstrdup(final_commit_name);
 }
 
 static int blame_copy_callback(const struct option *option, const char *arg, int unset)
@@ -2489,7 +2489,7 @@ int cmd_blame(int argc, const char **argv, const char *prefix)
 	struct origin *o;
 	struct blame_entry *ent = NULL;
 	long dashdash_pos, lno;
-	const char *final_commit_name = NULL;
+	char *final_commit_name = NULL;
 	enum object_type type;
 
 	static struct string_list range_list;
@@ -2786,6 +2786,8 @@ parse_done:
 
 	assign_blame(&sb, opt);
 
+	free(final_commit_name);
+
 	if (incremental)
 		return 0;
 
-- 
2.2.1

^ permalink raw reply related	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2015-01-14 21:54 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-01-10 21:33 [PATCH] blame.c: fix garbled error message Lukas Fleischer
2015-01-12 19:08 ` Junio C Hamano
2015-01-12 20:40   ` Jeff King
2015-01-12 22:55   ` Junio C Hamano
2015-01-12 23:12     ` Jeff King
2015-01-13  0:11       ` Junio C Hamano
2015-01-13  1:54         ` Jeff King
2015-01-13  1:57           ` [PATCH 1/5] git-compat-util: add xstrdup_or_null helper Jeff King
2015-01-13  2:21             ` Jonathan Nieder
2015-01-13  2:23               ` Jeff King
2015-01-13  1:58           ` [PATCH 2/5] builtin/apply.c: use xstrdup_or_null instead of null_strdup Jeff King
2015-01-13  1:58           ` [PATCH 3/5] builtin/commit.c: use xstrdup_or_null instead of envdup Jeff King
2015-01-13  1:59           ` [PATCH 4/5] use xstrdup_or_null to replace ternary conditionals Jeff King
2015-01-13  1:59           ` [PATCH 5/5] blame.c: fix garbled error message Jeff King
2015-01-14 14:21           ` [PATCH] " Lukas Fleischer
2015-01-14 17:22             ` Junio C Hamano
2015-01-14 20:49               ` Jeff King
2015-01-14 21:54                 ` Junio C Hamano
2015-01-12 23:18     ` Lukas Fleischer

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.