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 <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Oct 24, 2011 at 17:36 UTC
0de1633783685e9fb1943551217cdda7edbd245b
7 files changed
+20
-20
builtin/grep.c
+1
-1
@@ -547,7 +547,7 @@ static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
547
int old_baselen = base->len;
548
549
while (tree_entry(tree, &entry)) {
550
- int te_len = tree_entry_len(entry.path, entry.sha1);
550
+ int te_len = tree_entry_len(&entry);
551
552
if (match != 2) {
553
match = tree_entry_interesting(&entry, base, tn_len, pathspec);
builtin/pack-objects.c
+1
-1
@@ -979,7 +979,7 @@ static void add_pbase_object(struct tree_desc *tree,
979
while (tree_entry(tree,&entry)) {
980
if (S_ISGITLINK(entry.mode))
981
continue;
982
- cmp = tree_entry_len(entry.path, entry.sha1) != cmplen ? 1 :
982
+ cmp = tree_entry_len(&entry) != cmplen ? 1 :
983
memcmp(name, entry.path, cmplen);
984
if (cmp > 0)
985
continue;
tree-diff.c
+3
-3
@@ -21,8 +21,8 @@ static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2,
21
sha1 = tree_entry_extract(t1, &path1, &mode1);
22
sha2 = tree_entry_extract(t2, &path2, &mode2);
23
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);
26
cmp = base_name_compare(path1, pathlen1, mode1, path2, pathlen2, mode2);
27
if (cmp < 0) {
28
show_entry(opt, "-", t1, base);
@@ -85,7 +85,7 @@ static void show_entry(struct diff_options *opt, const char *prefix,
85
unsigned mode;
86
const char *path;
87
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);
89
int old_baselen = base->len;
90
91
strbuf_add(base, path, pathlen);
tree-walk.c
+8
-8
@@ -116,7 +116,7 @@ void setup_traverse_info(struct traverse_info *info, const char *base)
116
117
char *make_traverse_path(char *path, const struct traverse_info *info, const struct name_entry *n)
118
{
119
- int len = tree_entry_len(n->path, n->sha1);
119
+ int len = tree_entry_len(n);
120
int pathlen = info->pathlen;
121
122
path[pathlen + len] = 0;
@@ -126,7 +126,7 @@ char *make_traverse_path(char *path, const struct traverse_info *info, const str
126
break;
127
path[--pathlen] = '/';
128
n = &info->name;
129
- len = tree_entry_len(n->path, n->sha1);
129
+ len = tree_entry_len(n);
130
info = info->prev;
131
pathlen -= len;
132
}
@@ -253,7 +253,7 @@ static void extended_entry_extract(struct tree_desc_x *t,
253
* The caller wants "first" from this tree, or nothing.
254
*/
255
path = a->path;
256
- len = tree_entry_len(a->path, a->sha1);
256
+ len = tree_entry_len(a);
257
switch (check_entry_match(first, first_len, path, len)) {
258
case -1:
259
entry_clear(a);
@@ -271,7 +271,7 @@ static void extended_entry_extract(struct tree_desc_x *t,
271
while (probe.size) {
272
entry_extract(&probe, a);
273
path = a->path;
274
- len = tree_entry_len(a->path, a->sha1);
274
+ len = tree_entry_len(a);
275
switch (check_entry_match(first, first_len, path, len)) {
276
case -1:
277
entry_clear(a);
@@ -362,7 +362,7 @@ int traverse_trees(int n, struct tree_desc *t, struct traverse_info *info)
362
e = entry + i;
363
if (!e->path)
364
continue;
365
- len = tree_entry_len(e->path, e->sha1);
365
+ len = tree_entry_len(e);
366
if (!first) {
367
first = e->path;
368
first_len = len;
@@ -381,7 +381,7 @@ int traverse_trees(int n, struct tree_desc *t, struct traverse_info *info)
381
/* Cull the ones that are not the earliest */
382
if (!e->path)
383
continue;
384
- len = tree_entry_len(e->path, e->sha1);
384
+ len = tree_entry_len(e);
385
if (name_compare(e->path, len, first, first_len))
386
entry_clear(e);
387
}
@@ -434,8 +434,8 @@ static int find_tree_entry(struct tree_desc *t, const char *name, unsigned char
434
int entrylen, cmp;
435
436
sha1 = tree_entry_extract(t, &entry, mode);
437
+ entrylen = tree_entry_len(&t->entry);
438
update_tree_entry(t);
438
- entrylen = tree_entry_len(entry, sha1);
439
if (entrylen > namelen)
440
continue;
441
cmp = memcmp(name, entry, entrylen);
@@ -596,7 +596,7 @@ int tree_entry_interesting(const struct name_entry *entry,
596
ps->max_depth);
597
}
598
599
- pathlen = tree_entry_len(entry->path, entry->sha1);
599
+ pathlen = tree_entry_len(entry);
600
601
for (i = ps->nr - 1; i >= 0; i--) {
602
const struct pathspec_item *item = ps->items+i;
tree-walk.h
+3
-3
@@ -20,9 +20,9 @@ static inline const unsigned char *tree_entry_extract(struct tree_desc *desc, co
20
return desc->entry.sha1;
21
}
22
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)
24
{
25
- return (const char *)sha1 - name - 1;
25
+ return (const char *)ne->sha1 - ne->path - 1;
26
}
27
28
void update_tree_entry(struct tree_desc *);
@@ -58,7 +58,7 @@ extern void setup_traverse_info(struct traverse_info *info, const char *base);
58
59
static inline int traverse_path_len(const struct traverse_info *info, const struct name_entry *n)
60
{
61
- return info->pathlen + tree_entry_len(n->path, n->sha1);
61
+ return info->pathlen + tree_entry_len(n);
62
}
63
64
extern int tree_entry_interesting(const struct name_entry *, struct strbuf *, int, const struct pathspec *ps);
tree.c
+1
-1
@@ -99,7 +99,7 @@ static int read_tree_1(struct tree *tree, struct strbuf *base,
99
else
100
continue;
101
102
- len = tree_entry_len(entry.path, entry.sha1);
102
+ len = tree_entry_len(&entry);
103
strbuf_add(base, entry.path, len);
104
strbuf_addch(base, '/');
105
retval = read_tree_1(lookup_tree(sha1),
unpack-trees.c
+3
-3
@@ -446,7 +446,7 @@ static int traverse_trees_recursive(int n, unsigned long dirmask,
446
newinfo.prev = info;
447
newinfo.pathspec = info->pathspec;
448
newinfo.name = *p;
449
- newinfo.pathlen += tree_entry_len(p->path, p->sha1) + 1;
449
+ newinfo.pathlen += tree_entry_len(p) + 1;
450
newinfo.conflicts |= df_conflicts;
451
452
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_
495
ce_len -= pathlen;
496
ce_name = ce->name + pathlen;
497
498
- len = tree_entry_len(n->path, n->sha1);
498
+ len = tree_entry_len(n);
499
return df_name_compare(ce_name, ce_len, S_IFREG, n->path, len, n->mode);
500
}
501
@@ -626,7 +626,7 @@ static int find_cache_pos(struct traverse_info *info,
626
struct unpack_trees_options *o = info->data;
627
struct index_state *index = o->src_index;
628
int pfxlen = info->pathlen;
629
- int p_len = tree_entry_len(p->path, p->sha1);
629
+ int p_len = tree_entry_len(p);
630
631
for (pos = o->cache_bottom; pos < index->cache_nr; pos++) {
632
struct cache_entry *ce = index->cache[pos];