Skip to content

Commit 662174d

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d31f3ad commit 662174d

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

bisect.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,12 @@ static struct commit_list *best_bisection_sorted(struct commit_list *list, int n
216216
}
217217
qsort(array, cnt, sizeof(*array), compare_commit_dist);
218218
for (p = list, i = 0; i < cnt; i++) {
219-
struct name_decoration *r = xmalloc(sizeof(*r) + 100);
219+
char buf[100]; /* enough for dist=%d */
220220
struct object *obj = &(array[i].commit->object);
221221

222-
sprintf(r->name, "dist=%d", array[i].distance);
223-
r->next = add_decoration(&name_decoration, obj, r);
222+
snprintf(buf, sizeof(buf), "dist=%d", array[i].distance);
223+
add_name_decoration(DECORATION_NONE, buf, obj);
224+
224225
p->item = array[i].commit;
225226
p = p->next;
226227
}

commit.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ struct name_decoration {
3434
char name[1];
3535
};
3636

37+
enum decoration_type {
38+
DECORATION_NONE = 0,
39+
DECORATION_REF_LOCAL,
40+
DECORATION_REF_REMOTE,
41+
DECORATION_REF_TAG,
42+
DECORATION_REF_STASH,
43+
DECORATION_REF_HEAD,
44+
DECORATION_GRAFTED,
45+
};
46+
47+
void add_name_decoration(enum decoration_type type, const char *name, struct object *obj);
48+
3749
struct commit *lookup_commit(const unsigned char *sha1);
3850
struct commit *lookup_commit_reference(const unsigned char *sha1);
3951
struct commit *lookup_commit_reference_gently(const unsigned char *sha1,

log-tree.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,6 @@
1414

1515
struct decoration name_decoration = { "object names" };
1616

17-
enum decoration_type {
18-
DECORATION_NONE = 0,
19-
DECORATION_REF_LOCAL,
20-
DECORATION_REF_REMOTE,
21-
DECORATION_REF_TAG,
22-
DECORATION_REF_STASH,
23-
DECORATION_REF_HEAD,
24-
DECORATION_GRAFTED,
25-
};
26-
2717
static char decoration_colors[][COLOR_MAXLEN] = {
2818
GIT_COLOR_RESET,
2919
GIT_COLOR_BOLD_GREEN, /* REF_LOCAL */
@@ -84,7 +74,7 @@ int parse_decorate_color_config(const char *var, const int ofs, const char *valu
8474
#define decorate_get_color_opt(o, ix) \
8575
decorate_get_color((o)->use_color, ix)
8676

87-
static void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
77+
void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
8878
{
8979
int nlen = strlen(name);
9080
struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + nlen);

0 commit comments

Comments
 (0)