Skip to content

Commit aa21cc9

Browse files
committed
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
2 parents 8833260 + 5290d45 commit aa21cc9

File tree

5 files changed

+16
-8
lines changed

5 files changed

+16
-8
lines changed

path.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,8 @@ const char *remove_leading_path(const char *in, const char *prefix)
10771077

10781078
/*
10791079
* It is okay if dst == src, but they should not overlap otherwise.
1080+
* The "dst" buffer must be at least as long as "src"; normalizing may shrink
1081+
* the size of the path, but will never grow it.
10801082
*
10811083
* Performs the following normalizations on src, storing the result in dst:
10821084
* - Ensures that components are separated by '/' (Windows only)

tree-walk.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "cache.h"
22
#include "tree-walk.h"
3-
#include "unpack-trees.h"
43
#include "dir.h"
54
#include "object-store.h"
65
#include "tree.h"
@@ -410,15 +409,20 @@ int traverse_trees(struct index_state *istate,
410409
struct traverse_info *info)
411410
{
412411
int error = 0;
413-
struct name_entry *entry = xmalloc(n*sizeof(*entry));
412+
struct name_entry entry[MAX_TRAVERSE_TREES];
414413
int i;
415-
struct tree_desc_x *tx = xcalloc(n, sizeof(*tx));
414+
struct tree_desc_x tx[ARRAY_SIZE(entry)];
416415
struct strbuf base = STRBUF_INIT;
417416
int interesting = 1;
418417
char *traverse_path;
419418

420-
for (i = 0; i < n; i++)
419+
if (n >= ARRAY_SIZE(entry))
420+
BUG("traverse_trees() called with too many trees (%d)", n);
421+
422+
for (i = 0; i < n; i++) {
421423
tx[i].d = t[i];
424+
tx[i].skip = NULL;
425+
}
422426

423427
if (info->prev) {
424428
strbuf_make_traverse_path(&base, info->prev,
@@ -506,10 +510,8 @@ int traverse_trees(struct index_state *istate,
506510
if (mask & (1ul << i))
507511
update_extended_entry(tx + i, entry + i);
508512
}
509-
free(entry);
510513
for (i = 0; i < n; i++)
511514
free_extended_entry(tx + i);
512-
free(tx);
513515
free(traverse_path);
514516
info->traverse_path = NULL;
515517
strbuf_release(&base);

tree-walk.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#include "cache.h"
55

6+
#define MAX_TRAVERSE_TREES 8
7+
68
/**
79
* The tree walking API is used to traverse and inspect trees.
810
*/

unpack-trees.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "string-list.h"
77
#include "tree-walk.h"
88

9-
#define MAX_UNPACK_TREES 8
9+
#define MAX_UNPACK_TREES MAX_TRAVERSE_TREES
1010

1111
struct cache_entry;
1212
struct unpack_trees_options;

walker.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,12 +261,14 @@ int walker_fetch(struct walker *walker, int targets, char **target,
261261
struct strbuf refname = STRBUF_INIT;
262262
struct strbuf err = STRBUF_INIT;
263263
struct ref_transaction *transaction = NULL;
264-
struct object_id *oids = xmalloc(targets * sizeof(struct object_id));
264+
struct object_id *oids;
265265
char *msg = NULL;
266266
int i, ret = -1;
267267

268268
save_commit_buffer = 0;
269269

270+
ALLOC_ARRAY(oids, targets);
271+
270272
if (write_ref) {
271273
transaction = ref_transaction_begin(&err);
272274
if (!transaction) {

0 commit comments

Comments
 (0)