Re: [PATCH 3/3] log-tree: use FLEX_ARRAY in name_decoration
On Tue, Aug 26, 2014 at 6:24 AM, Jeff King <peff@peff.net> wrote:
Show 8 quoted lines
> 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 putting into the flex-array (it did not
Show 40 quoted lines
> need to before, because the struct itself was over-allocated
> by one byte).
>
> Signed-off-by: Jeff King <peff@peff.net>
> ---
> This could come first in the series, but doing it last means we only
> have to update one spot. :)
>
> commit.h | 2 +-
> log-tree.c | 2 +-
> 2 files changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/commit.h b/commit.h
> index 263b49e..1516fc9 100644
> --- a/commit.h
> +++ b/commit.h
> @@ -29,7 +29,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 {
> diff --git a/log-tree.c b/log-tree.c
> index 7cbc4ee..fb60018 100644
> --- a/log-tree.c
> +++ b/log-tree.c
> @@ -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);
> --
> 2.1.0.346.ga0367b9