diff options
author | Jeff King <peff@peff.net> | 2019-07-31 00:38:23 -0400 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2019-08-01 13:06:52 -0700 |
commit | c43ab062598d0299ea6e0d115a6018189a7793bf (patch) | |
tree | 03abcb873feb9ab6d23d51f9e7f45d1c2b14af26 /unpack-trees.c | |
parent | b3b3cbcbf246b1051ad453bc02e24a89573e2911 (diff) | |
download | git-c43ab062598d0299ea6e0d115a6018189a7793bf.tar.gz |
tree-walk: add a strbuf wrapper for make_traverse_path()
All but one of the callers of make_traverse_path() allocate a new heap
buffer to store the path. Let's give them an easy way to write to a
strbuf, which saves them from computing the length themselves (which is
especially tricky when they want to add to the path). It will also make
it easier for us to change the make_traverse_path() interface in a
future patch to improve its bounds-checking.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'unpack-trees.c')
-rw-r--r-- | unpack-trees.c | 16 |
1 files changed, 7 insertions, 9 deletions
diff --git a/unpack-trees.c b/unpack-trees.c index 492eff666a..c3059c2440 100644 --- a/unpack-trees.c +++ b/unpack-trees.c @@ -686,21 +686,19 @@ static int index_pos_by_traverse_info(struct name_entry *names, struct traverse_info *info) { struct unpack_trees_options *o = info->data; - size_t len = traverse_path_len(info, tree_entry_len(names)); - char *name = xmalloc(len + 1 /* slash */ + 1 /* NUL */); + struct strbuf name = STRBUF_INIT; int pos; - make_traverse_path(name, info, names->path, names->pathlen); - name[len++] = '/'; - name[len] = '\0'; - pos = index_name_pos(o->src_index, name, len); + strbuf_make_traverse_path(&name, info, names->path, names->pathlen); + strbuf_addch(&name, '/'); + pos = index_name_pos(o->src_index, name.buf, name.len); if (pos >= 0) BUG("This is a directory and should not exist in index"); pos = -pos - 1; - if (!starts_with(o->src_index->cache[pos]->name, name) || - (pos > 0 && starts_with(o->src_index->cache[pos-1]->name, name))) + if (!starts_with(o->src_index->cache[pos]->name, name.buf) || + (pos > 0 && starts_with(o->src_index->cache[pos-1]->name, name.buf))) BUG("pos must point at the first entry in this directory"); - free(name); + strbuf_release(&name); return pos; } |