summaryrefslogtreecommitdiff
path: root/tree-walk.c
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2019-07-31 04:38:23 (GMT)
committerJunio C Hamano <gitster@pobox.com>2019-08-01 20:06:52 (GMT)
commitc43ab062598d0299ea6e0d115a6018189a7793bf (patch)
tree03abcb873feb9ab6d23d51f9e7f45d1c2b14af26 /tree-walk.c
parentb3b3cbcbf246b1051ad453bc02e24a89573e2911 (diff)
downloadgit-c43ab062598d0299ea6e0d115a6018189a7793bf.zip
git-c43ab062598d0299ea6e0d115a6018189a7793bf.tar.gz
git-c43ab062598d0299ea6e0d115a6018189a7793bf.tar.bz2
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 'tree-walk.c')
-rw-r--r--tree-walk.c21
1 files changed, 15 insertions, 6 deletions
diff --git a/tree-walk.c b/tree-walk.c
index 70f9eb5..c2952f3 100644
--- a/tree-walk.c
+++ b/tree-walk.c
@@ -200,6 +200,17 @@ char *make_traverse_path(char *path, const struct traverse_info *info,
return path;
}
+void strbuf_make_traverse_path(struct strbuf *out,
+ const struct traverse_info *info,
+ const char *name, size_t namelen)
+{
+ size_t len = traverse_path_len(info, namelen);
+
+ strbuf_grow(out, len);
+ make_traverse_path(out->buf + out->len, info, name, namelen);
+ strbuf_setlen(out, out->len + len);
+}
+
struct tree_desc_skip {
struct tree_desc_skip *prev;
const void *ptr;
@@ -396,12 +407,10 @@ int traverse_trees(struct index_state *istate,
tx[i].d = t[i];
if (info->prev) {
- strbuf_grow(&base, info->pathlen);
- make_traverse_path(base.buf, info->prev, info->name,
- info->namelen);
- base.buf[info->pathlen-1] = '/';
- strbuf_setlen(&base, info->pathlen);
- traverse_path = xstrndup(base.buf, info->pathlen);
+ strbuf_make_traverse_path(&base, info->prev,
+ info->name, info->namelen);
+ strbuf_addch(&base, '/');
+ traverse_path = xstrndup(base.buf, base.len);
} else {
traverse_path = xstrndup(info->name, info->pathlen);
}