From 662174d299a2221016a8756d35d485b576ebcec2 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Tue, 26 Aug 2014 06:23:36 -0400 Subject: log-tree: make add_name_decoration a public function The log-tree code keeps a "struct decoration" hash to show text decorations for each commit during log traversals. It makes this available to other files by providing global access to the hash. This can result in other code adding entries that do not conform to what log-tree expects. For example, the bisect code adds its own "dist" decorations to be shown. Originally the bisect code was correct, but when the name_decoration code grew a new field in eb3005e (commit.h: add 'type' to struct name_decoration, 2010-06-19), the bisect code was not updated. As a result, the log-tree code can access uninitialized memory and even segfault. We can fix this by making name_decoration's adding function public. If all callers use it, then any changes to struct initialization only need to happen in one place (and because the members come in as parameters, the compiler can notice a caller who does not supply enough information). As a bonus, this also means that the decoration hashes created by the bisect code will use less memory (previously we over-allocated space for the distance integer, but now we format it into a temporary buffer and copy it to the final flex-array). Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- commit.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'commit.h') diff --git a/commit.h b/commit.h index a9f177ba48..06c06df3ec 100644 --- a/commit.h +++ b/commit.h @@ -34,6 +34,18 @@ struct name_decoration { char name[1]; }; +enum decoration_type { + DECORATION_NONE = 0, + DECORATION_REF_LOCAL, + DECORATION_REF_REMOTE, + DECORATION_REF_TAG, + DECORATION_REF_STASH, + DECORATION_REF_HEAD, + DECORATION_GRAFTED, +}; + +void add_name_decoration(enum decoration_type type, const char *name, struct object *obj); + struct commit *lookup_commit(const unsigned char *sha1); struct commit *lookup_commit_reference(const unsigned char *sha1); struct commit *lookup_commit_reference_gently(const unsigned char *sha1, -- cgit v1.2.3 From 2608c24940c80bf379937e4cefee75e2db79e008 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Tue, 26 Aug 2014 06:23:54 -0400 Subject: log-tree: make name_decoration hash static In the previous commit, we made add_name_decoration global so that adders would not have to access the hash directly. We now make the hash itself static so that callers _have_ to add through our function, making sure that all additions go through a single point. To do this, we have to add one more accessor function: a way to lookup entries in the hash. Since the only caller doesn't actually look at the returned value, but rather only asks whether there is a decoration or not, we could provide only a boolean "has_name_decoration". That would allow us to make "struct name_decoration" local to log-tree, as well. However, it's unlikely to cause any maintainability harm making the actual data public, and this interface is more flexible if we need to look at decorations from other parts of the code in the future. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- commit.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'commit.h') diff --git a/commit.h b/commit.h index 06c06df3ec..f3d2f57a89 100644 --- a/commit.h +++ b/commit.h @@ -27,7 +27,6 @@ extern int save_commit_buffer; extern const char *commit_type; /* While we can decorate any object with a name, it's only used for commits.. */ -extern struct decoration name_decoration; struct name_decoration { struct name_decoration *next; int type; @@ -45,6 +44,7 @@ enum decoration_type { }; void add_name_decoration(enum decoration_type type, const char *name, struct object *obj); +const struct name_decoration *get_name_decoration(const struct object *obj); struct commit *lookup_commit(const unsigned char *sha1); struct commit *lookup_commit_reference(const unsigned char *sha1); -- cgit v1.2.3 From 2e3dfb216991974b60fdb1933eb3331e03383e61 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Tue, 26 Aug 2014 06:24:20 -0400 Subject: log-tree: use FLEX_ARRAY in name_decoration We are already using the flex-array technique; let's annotate it with our usual FLEX_ARRAY macro. Besides being more readable, this is slightly more efficient on compilers that understand flex-arrays. Note that we need to bump the allocation in add_name_decoration, which did not explicitly add one byte for the NUL terminator of the string we are putting into the flex-array (it did not need to before, because the struct itself was over-allocated by one byte). Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- commit.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'commit.h') diff --git a/commit.h b/commit.h index f3d2f57a89..99b9e78209 100644 --- a/commit.h +++ b/commit.h @@ -30,7 +30,7 @@ extern const char *commit_type; struct name_decoration { struct name_decoration *next; int type; - char name[1]; + char name[FLEX_ARRAY]; }; enum decoration_type { -- cgit v1.2.3