Skip to content

Commit 2e3dfb2

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 2608c24 commit 2e3dfb2

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

commit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ extern const char *commit_type;
3030
struct name_decoration {
3131
struct name_decoration *next;
3232
int type;
33-
char name[1];
33+
char name[FLEX_ARRAY];
3434
};
3535

3636
enum decoration_type {

log-tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ int parse_decorate_color_config(const char *var, const int ofs, const char *valu
7777
void add_name_decoration(enum decoration_type type, const char *name, struct object *obj)
7878
{
7979
int nlen = strlen(name);
80-
struct name_decoration *res = xmalloc(sizeof(struct name_decoration) + nlen);
80+
struct name_decoration *res = xmalloc(sizeof(*res) + nlen + 1);
8181
memcpy(res->name, name, nlen + 1);
8282
res->type = type;
8383
res->next = add_decoration(&name_decoration, obj, res);

0 commit comments

Comments
 (0)