From: Elijah Newren <newren@gmail.com>
To: Jeff King <peff@peff.net>
Cc: Git Mailing List <git@vger.kernel.org>
Subject: Re: [PATCH 3/3] traverse_trees(): use stack array for name entries
Date: Thu, 30 Jan 2020 06:57:26 -0800 [thread overview]
Message-ID: <CABPp-BE7E--8Yz3PAcjPQX2RCsbq0Q0gURi3RJuE64KM0eo6mA@mail.gmail.com> (raw)
In-Reply-To: <20200130095338.GC840531@coredump.intra.peff.net>
On Thu, Jan 30, 2020 at 1:54 AM Jeff King <peff@peff.net> wrote:
>
> We heap-allocate our arrays of name_entry structs, etc, with one entry
> per tree we're asked to traverse. The code does a raw multiplication in
> the xmalloc() call, which I find when auditing for integer overflows
> during allocation.
>
> We could "fix" this by using ALLOC_ARRAY() instead. But as it turns out,
> the maximum size of these arrays is limited at compile time:
>
> - merge_trees() always passes in 3 trees
>
> - unpack_trees() and its brethren never pass in more than
> MAX_UNPACK_TREES
>
> So we can simplify even further by just using a stack array and bounding
> it with MAX_UNPACK_TREES. There should be no concern with overflowing
> the stack, since MAX_UNPACK_TREES is only 8 and the structs themselves
> are small.
>
> Note that since we're replacing xcalloc(), we have to move one of the
> NULL initializations into a loop.
>
> Signed-off-by: Jeff King <peff@peff.net>
> ---
>
> This does increase the coupling between tree-walk and unpack-trees a
> bit. I'd be OK just switching to ALLOC_ARRAY(), too. I doubt the
> performance improvement is measurable, and the cleanup free() calls are
> already there.
Could we undo this cyclic dependency between tree-walk and
unpack-trees by defining MAX_TRAVERSE_TREES in tree-walk.h, making
MAX_UNPACK_TREES in unpack-trees.h be defined to MAX_TRAVERSE_TREES,
and remove the include of unpack-trees.h in tree-walk.c?
> tree-walk.c | 13 ++++++++-----
> 1 file changed, 8 insertions(+), 5 deletions(-)
>
> diff --git a/tree-walk.c b/tree-walk.c
> index d5a8e096a6..3093cf7098 100644
> --- a/tree-walk.c
> +++ b/tree-walk.c
> @@ -410,15 +410,20 @@ int traverse_trees(struct index_state *istate,
> struct traverse_info *info)
> {
> int error = 0;
> - struct name_entry *entry = xmalloc(n*sizeof(*entry));
> + struct name_entry entry[MAX_UNPACK_TREES];
> int i;
> - struct tree_desc_x *tx = xcalloc(n, sizeof(*tx));
> + struct tree_desc_x tx[ARRAY_SIZE(entry)];
> struct strbuf base = STRBUF_INIT;
> int interesting = 1;
> char *traverse_path;
>
> - for (i = 0; i < n; i++)
> + if (n >= ARRAY_SIZE(entry))
> + BUG("traverse_trees() called with too many trees (%d)", n);
> +
> + for (i = 0; i < n; i++) {
> tx[i].d = t[i];
> + tx[i].skip = NULL;
> + }
>
> if (info->prev) {
> strbuf_make_traverse_path(&base, info->prev,
> @@ -506,10 +511,8 @@ int traverse_trees(struct index_state *istate,
> if (mask & (1ul << i))
> update_extended_entry(tx + i, entry + i);
> }
> - free(entry);
> for (i = 0; i < n; i++)
> free_extended_entry(tx + i);
> - free(tx);
> free(traverse_path);
> info->traverse_path = NULL;
> strbuf_release(&base);
> --
> 2.25.0.515.gaba5347bc6
Looks good to me otherwise.
next prev parent reply other threads:[~2020-01-30 14:57 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-01-30 9:51 [PATCH 0/3] some minor memory allocation cleanups Jeff King
2020-01-30 9:52 ` [PATCH 1/3] normalize_path_copy(): document "dst" size expectations Jeff King
2020-01-30 20:12 ` Taylor Blau
2020-01-31 8:45 ` Jeff King
2020-01-30 9:52 ` [PATCH 2/3] walker_fetch(): avoid raw array length computation Jeff King
2020-01-30 9:53 ` [PATCH 3/3] traverse_trees(): use stack array for name entries Jeff King
2020-01-30 14:57 ` Elijah Newren [this message]
2020-01-31 9:29 ` Jeff King
2020-01-31 18:52 ` Elijah Newren
2020-02-01 11:39 ` [PATCH] tree-walk.c: break circular dependency with unpack-trees Jeff King
2020-02-01 15:32 ` Elijah Newren
2020-01-30 14:59 ` [PATCH 0/3] some minor memory allocation cleanups Elijah Newren
2020-01-30 23:03 ` Taylor Blau
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CABPp-BE7E--8Yz3PAcjPQX2RCsbq0Q0gURi3RJuE64KM0eo6mA@mail.gmail.com \
--to=newren@gmail.com \
--cc=git@vger.kernel.org \
--cc=peff@peff.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).