1
0
Fork 0
mirror of https://github.com/git/git.git synced 2024-04-27 10:35:08 +02:00

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 <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jeff King 2014-08-26 06:23:36 -04:00 committed by Junio C Hamano
parent d31f3ad23d
commit 662174d299
3 changed files with 17 additions and 14 deletions

View File

@ -216,11 +216,12 @@ static struct commit_list *best_bisection_sorted(struct commit_list *list, int n
}
qsort(array, cnt, sizeof(*array), compare_commit_dist);
for (p = list, i = 0; i < cnt; i++) {
struct name_decoration *r = xmalloc(sizeof(*r) + 100);
char buf[100]; /* enough for dist=%d */
struct object *obj = &(array[i].commit->object);
sprintf(r->name, "dist=%d", array[i].distance);
r->next = add_decoration(&name_decoration, obj, r);
snprintf(buf, sizeof(buf), "dist=%d", array[i].distance);
add_name_decoration(DECORATION_NONE, buf, obj);
p->item = array[i].commit;
p = p->next;
}

View File

@ -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,

View File

@ -14,16 +14,6 @@
struct decoration name_decoration = { "object names" };
enum decoration_type {
DECORATION_NONE = 0,
DECORATION_REF_LOCAL,
DECORATION_REF_REMOTE,
DECORATION_REF_TAG,
DECORATION_REF_STASH,
DECORATION_REF_HEAD,
DECORATION_GRAFTED,
};
static char decoration_colors[][COLOR_MAXLEN] = {
GIT_COLOR_RESET,
GIT_COLOR_BOLD_GREEN, /* REF_LOCAL */
@ -84,7 +74,7 @@ int parse_decorate_color_config(const char *var, const int ofs, const char *valu
#define decorate_get_color_opt(o, ix) \
decorate_get_color((o)->use_color, ix)
static void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
{
int nlen = strlen(name);
struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + nlen);