log-tree: use FLEX_ARRAY in name_decoration
authorJeff King <peff@peff.net>
Tue, 26 Aug 2014 10:24:20 +0000 (06:24 -0400)
committerJunio C Hamano <gitster@pobox.com>
Wed, 27 Aug 2014 14:44:27 +0000 (07:44 -0700)
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 <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
commit.h
log-tree.c
index f3d2f57a89fb640f5540a46e95ac680085c89fa5..99b9e78209d391b548f0e59239b63299bffaf300 100644 (file)
--- 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 {
index 2adf82ba02a9898a253087f073c1191bd55aa492..de760db1a630463980c2d52e8ee39322d9bd901f 100644 (file)
@@ -77,7 +77,7 @@ int parse_decorate_color_config(const char *var, const int ofs, const char *valu
 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);
+       struct name_decoration *res = xmalloc(sizeof(*res) + nlen + 1);
        memcpy(res->name, name, nlen + 1);
        res->type = type;
        res->next = add_decoration(&name_decoration, obj, res);