diff options
author | Junio C Hamano <gitster@pobox.com> | 2020-02-14 20:54:23 (GMT) |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2020-02-14 20:54:23 (GMT) |
commit | aa21cc97bd10ab0b62f6d951ff2ff725261e326e (patch) | |
tree | 69e983408fd1a4f599ea00d6b70599a00282be06 | |
parent | 883326077a1995a5ea122c298ff47fcd85782c49 (diff) | |
parent | 5290d4513496d89f84570985a0e02e97dff477ff (diff) | |
download | git-aa21cc97bd10ab0b62f6d951ff2ff725261e326e.zip git-aa21cc97bd10ab0b62f6d951ff2ff725261e326e.tar.gz git-aa21cc97bd10ab0b62f6d951ff2ff725261e326e.tar.bz2 |
Merge branch 'jk/alloc-cleanups'
Misc code clean-ups.
* jk/alloc-cleanups:
tree-walk.c: break circular dependency with unpack-trees
traverse_trees(): use stack array for name entries
walker_fetch(): avoid raw array length computation
normalize_path_copy(): document "dst" size expectations
-rw-r--r-- | path.c | 2 | ||||
-rw-r--r-- | tree-walk.c | 14 | ||||
-rw-r--r-- | tree-walk.h | 2 | ||||
-rw-r--r-- | unpack-trees.h | 2 | ||||
-rw-r--r-- | walker.c | 4 |
5 files changed, 16 insertions, 8 deletions
@@ -1077,6 +1077,8 @@ const char *remove_leading_path(const char *in, const char *prefix) /* * It is okay if dst == src, but they should not overlap otherwise. + * The "dst" buffer must be at least as long as "src"; normalizing may shrink + * the size of the path, but will never grow it. * * Performs the following normalizations on src, storing the result in dst: * - Ensures that components are separated by '/' (Windows only) diff --git a/tree-walk.c b/tree-walk.c index d5a8e09..bb0ad34 100644 --- a/tree-walk.c +++ b/tree-walk.c @@ -1,6 +1,5 @@ #include "cache.h" #include "tree-walk.h" -#include "unpack-trees.h" #include "dir.h" #include "object-store.h" #include "tree.h" @@ -410,15 +409,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_TRAVERSE_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 +510,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); diff --git a/tree-walk.h b/tree-walk.h index 826396c..a505846 100644 --- a/tree-walk.h +++ b/tree-walk.h @@ -3,6 +3,8 @@ #include "cache.h" +#define MAX_TRAVERSE_TREES 8 + /** * The tree walking API is used to traverse and inspect trees. */ diff --git a/unpack-trees.h b/unpack-trees.h index ca94a42..ae1557f 100644 --- a/unpack-trees.h +++ b/unpack-trees.h @@ -6,7 +6,7 @@ #include "string-list.h" #include "tree-walk.h" -#define MAX_UNPACK_TREES 8 +#define MAX_UNPACK_TREES MAX_TRAVERSE_TREES struct cache_entry; struct unpack_trees_options; @@ -261,12 +261,14 @@ int walker_fetch(struct walker *walker, int targets, char **target, struct strbuf refname = STRBUF_INIT; struct strbuf err = STRBUF_INIT; struct ref_transaction *transaction = NULL; - struct object_id *oids = xmalloc(targets * sizeof(struct object_id)); + struct object_id *oids; char *msg = NULL; int i, ret = -1; save_commit_buffer = 0; + ALLOC_ARRAY(oids, targets); + if (write_ref) { transaction = ref_transaction_begin(&err); if (!transaction) { |