Skip to content

Commit 0de1633

Browse files
pcloudsgitster
authored andcommitted
tree-walk.c: do not leak internal structure in tree_entry_len()
tree_entry_len() does not simply take two random arguments and return a tree length. The two pointers must point to a tree item structure, or struct name_entry. Passing random pointers will return incorrect value. Force callers to pass struct name_entry instead of two pointers (with hope that they don't manually construct struct name_entry themselves) Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 997a194 commit 0de1633

File tree

7 files changed

+20
-20
lines changed

7 files changed

+20
-20
lines changed

builtin/grep.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
547547
int old_baselen = base->len;
548548

549549
while (tree_entry(tree, &entry)) {
550-
int te_len = tree_entry_len(entry.path, entry.sha1);
550+
int te_len = tree_entry_len(&entry);
551551

552552
if (match != 2) {
553553
match = tree_entry_interesting(&entry, base, tn_len, pathspec);

builtin/pack-objects.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -979,7 +979,7 @@ static void add_pbase_object(struct tree_desc *tree,
979979
while (tree_entry(tree,&entry)) {
980980
if (S_ISGITLINK(entry.mode))
981981
continue;
982-
cmp = tree_entry_len(entry.path, entry.sha1) != cmplen ? 1 :
982+
cmp = tree_entry_len(&entry) != cmplen ? 1 :
983983
memcmp(name, entry.path, cmplen);
984984
if (cmp > 0)
985985
continue;

tree-diff.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2,
2121
sha1 = tree_entry_extract(t1, &path1, &mode1);
2222
sha2 = tree_entry_extract(t2, &path2, &mode2);
2323

24-
pathlen1 = tree_entry_len(path1, sha1);
25-
pathlen2 = tree_entry_len(path2, sha2);
24+
pathlen1 = tree_entry_len(&t1->entry);
25+
pathlen2 = tree_entry_len(&t2->entry);
2626
cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
2727
if (cmp < 0) {
2828
show_entry(opt, "-", t1, base);
@@ -85,7 +85,7 @@ static void show_entry(struct diff_options *opt, const char *prefix,
8585
unsigned mode;
8686
const char *path;
8787
const unsigned char *sha1 = tree_entry_extract(desc, &path, &mode);
88-
int pathlen = tree_entry_len(path, sha1);
88+
int pathlen = tree_entry_len(&desc->entry);
8989
int old_baselen = base->len;
9090

9191
strbuf_add(base, path, pathlen);

tree-walk.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ void setup_traverse_info(struct traverse_info *info, const char *base)
116116

117117
char *make_traverse_path(char *path, const struct traverse_info *info, const struct name_entry *n)
118118
{
119-
int len = tree_entry_len(n->path, n->sha1);
119+
int len = tree_entry_len(n);
120120
int pathlen = info->pathlen;
121121

122122
path[pathlen + len] = 0;
@@ -126,7 +126,7 @@ char *make_traverse_path(char *path, const struct traverse_info *info, const str
126126
break;
127127
path[--pathlen] = '/';
128128
n = &info->name;
129-
len = tree_entry_len(n->path, n->sha1);
129+
len = tree_entry_len(n);
130130
info = info->prev;
131131
pathlen -= len;
132132
}
@@ -253,7 +253,7 @@ static void extended_entry_extract(struct tree_desc_x *t,
253253
* The caller wants "first" from this tree, or nothing.
254254
*/
255255
path = a->path;
256-
len = tree_entry_len(a->path, a->sha1);
256+
len = tree_entry_len(a);
257257
switch (check_entry_match(first, first_len, path, len)) {
258258
case -1:
259259
entry_clear(a);
@@ -271,7 +271,7 @@ static void extended_entry_extract(struct tree_desc_x *t,
271271
while (probe.size) {
272272
entry_extract(&probe, a);
273273
path = a->path;
274-
len = tree_entry_len(a->path, a->sha1);
274+
len = tree_entry_len(a);
275275
switch (check_entry_match(first, first_len, path, len)) {
276276
case -1:
277277
entry_clear(a);
@@ -362,7 +362,7 @@ int traverse_trees(int n, struct tree_desc *t, struct traverse_info *info)
362362
e = entry + i;
363363
if (!e->path)
364364
continue;
365-
len = tree_entry_len(e->path, e->sha1);
365+
len = tree_entry_len(e);
366366
if (!first) {
367367
first = e->path;
368368
first_len = len;
@@ -381,7 +381,7 @@ int traverse_trees(int n, struct tree_desc *t, struct traverse_info *info)
381381
/* Cull the ones that are not the earliest */
382382
if (!e->path)
383383
continue;
384-
len = tree_entry_len(e->path, e->sha1);
384+
len = tree_entry_len(e);
385385
if (name_compare(e->path, len, first, first_len))
386386
entry_clear(e);
387387
}
@@ -434,8 +434,8 @@ static int find_tree_entry(struct tree_desc *t, const char *name, unsigned char
434434
int entrylen, cmp;
435435

436436
sha1 = tree_entry_extract(t, &entry, mode);
437+
entrylen = tree_entry_len(&t->entry);
437438
update_tree_entry(t);
438-
entrylen = tree_entry_len(entry, sha1);
439439
if (entrylen > namelen)
440440
continue;
441441
cmp = memcmp(name, entry, entrylen);
@@ -596,7 +596,7 @@ int tree_entry_interesting(const struct name_entry *entry,
596596
ps->max_depth);
597597
}
598598

599-
pathlen = tree_entry_len(entry->path, entry->sha1);
599+
pathlen = tree_entry_len(entry);
600600

601601
for (i = ps->nr - 1; i >= 0; i--) {
602602
const struct pathspec_item *item = ps->items+i;

tree-walk.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ static inline const unsigned char *tree_entry_extract(struct tree_desc *desc, co
2020
return desc->entry.sha1;
2121
}
2222

23-
static inline int tree_entry_len(const char *name, const unsigned char *sha1)
23+
static inline int tree_entry_len(const struct name_entry *ne)
2424
{
25-
return (const char *)sha1 - name - 1;
25+
return (const char *)ne->sha1 - ne->path - 1;
2626
}
2727

2828
void update_tree_entry(struct tree_desc *);
@@ -58,7 +58,7 @@ extern void setup_traverse_info(struct traverse_info *info, const char *base);
5858

5959
static inline int traverse_path_len(const struct traverse_info *info, const struct name_entry *n)
6060
{
61-
return info->pathlen + tree_entry_len(n->path, n->sha1);
61+
return info->pathlen + tree_entry_len(n);
6262
}
6363

6464
extern int tree_entry_interesting(const struct name_entry *, struct strbuf *, int, const struct pathspec *ps);

tree.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ static int read_tree_1(struct tree *tree, struct strbuf *base,
9999
else
100100
continue;
101101

102-
len = tree_entry_len(entry.path, entry.sha1);
102+
len = tree_entry_len(&entry);
103103
strbuf_add(base, entry.path, len);
104104
strbuf_addch(base, '/');
105105
retval = read_tree_1(lookup_tree(sha1),

unpack-trees.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ static int traverse_trees_recursive(int n, unsigned long dirmask,
446446
newinfo.prev = info;
447447
newinfo.pathspec = info->pathspec;
448448
newinfo.name = *p;
449-
newinfo.pathlen += tree_entry_len(p->path, p->sha1) + 1;
449+
newinfo.pathlen += tree_entry_len(p) + 1;
450450
newinfo.conflicts |= df_conflicts;
451451

452452
for (i = 0; i < n; i++, dirmask >>= 1) {
@@ -495,7 +495,7 @@ static int do_compare_entry(const struct cache_entry *ce, const struct traverse_
495495
ce_len -= pathlen;
496496
ce_name = ce->name + pathlen;
497497

498-
len = tree_entry_len(n->path, n->sha1);
498+
len = tree_entry_len(n);
499499
return df_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode);
500500
}
501501

@@ -626,7 +626,7 @@ static int find_cache_pos(struct traverse_info *info,
626626
struct unpack_trees_options *o = info->data;
627627
struct index_state *index = o->src_index;
628628
int pfxlen = info->pathlen;
629-
int p_len = tree_entry_len(p->path, p->sha1);
629+
int p_len = tree_entry_len(p);
630630

631631
for (pos = o->cache_bottom; pos < index->cache_nr; pos++) {
632632
struct cache_entry *ce = index->cache[pos];

0 commit comments

Comments
 (0)