Skip to content

Commit df6246e

Browse files
committed
Merge branch 'nd/misc-cleanups' into maint
* nd/misc-cleanups: unpack_object_header_buffer(): clear the size field upon error tree_entry_interesting: make use of local pointer "item" tree_entry_interesting(): give meaningful names to return values read_directory_recursive: reduce one indentation level get_tree_entry(): do not call find_tree_entry() on an empty tree tree-walk.c: do not leak internal structure in tree_entry_len()
2 parents 8311158 + ea4f968 commit df6246e

File tree

10 files changed

+112
-95
lines changed

10 files changed

+112
-95
lines changed

builtin/grep.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -557,18 +557,19 @@ static int grep_cache(struct grep_opt *opt, const struct pathspec *pathspec, int
557557
static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
558558
struct tree_desc *tree, struct strbuf *base, int tn_len)
559559
{
560-
int hit = 0, match = 0;
560+
int hit = 0;
561+
enum interesting match = entry_not_interesting;
561562
struct name_entry entry;
562563
int old_baselen = base->len;
563564

564565
while (tree_entry(tree, &entry)) {
565-
int te_len = tree_entry_len(entry.path, entry.sha1);
566+
int te_len = tree_entry_len(&entry);
566567

567-
if (match != 2) {
568+
if (match != all_entries_interesting) {
568569
match = tree_entry_interesting(&entry, base, tn_len, pathspec);
569-
if (match < 0)
570+
if (match == all_entries_not_interesting)
570571
break;
571-
if (match == 0)
572+
if (match == entry_not_interesting)
572573
continue;
573574
}
574575

builtin/pack-objects.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1015,7 +1015,7 @@ static void add_pbase_object(struct tree_desc *tree,
10151015
while (tree_entry(tree,&entry)) {
10161016
if (S_ISGITLINK(entry.mode))
10171017
continue;
1018-
cmp = tree_entry_len(entry.path, entry.sha1) != cmplen ? 1 :
1018+
cmp = tree_entry_len(&entry) != cmplen ? 1 :
10191019
memcmp(name, entry.path, cmplen);
10201020
if (cmp > 0)
10211021
continue;

dir.c

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -968,34 +968,34 @@ static int read_directory_recursive(struct dir_struct *dir,
968968
{
969969
DIR *fdir = opendir(*base ? base : ".");
970970
int contents = 0;
971+
struct dirent *de;
972+
char path[PATH_MAX + 1];
971973

972-
if (fdir) {
973-
struct dirent *de;
974-
char path[PATH_MAX + 1];
975-
memcpy(path, base, baselen);
976-
977-
while ((de = readdir(fdir)) != NULL) {
978-
int len;
979-
switch (treat_path(dir, de, path, sizeof(path),
980-
baselen, simplify, &len)) {
981-
case path_recurse:
982-
contents += read_directory_recursive
983-
(dir, path, len, 0, simplify);
984-
continue;
985-
case path_ignored:
986-
continue;
987-
case path_handled:
988-
break;
989-
}
990-
contents++;
991-
if (check_only)
992-
goto exit_early;
993-
else
994-
dir_add_name(dir, path, len);
974+
if (!fdir)
975+
return 0;
976+
977+
memcpy(path, base, baselen);
978+
979+
while ((de = readdir(fdir)) != NULL) {
980+
int len;
981+
switch (treat_path(dir, de, path, sizeof(path),
982+
baselen, simplify, &len)) {
983+
case path_recurse:
984+
contents += read_directory_recursive(dir, path, len, 0, simplify);
985+
continue;
986+
case path_ignored:
987+
continue;
988+
case path_handled:
989+
break;
995990
}
996-
exit_early:
997-
closedir(fdir);
991+
contents++;
992+
if (check_only)
993+
goto exit_early;
994+
else
995+
dir_add_name(dir, path, len);
998996
}
997+
exit_early:
998+
closedir(fdir);
999999

10001000
return contents;
10011001
}

list-objects.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ static void process_tree(struct rev_info *revs,
7171
struct tree_desc desc;
7272
struct name_entry entry;
7373
struct name_path me;
74-
int match = revs->diffopt.pathspec.nr == 0 ? 2 : 0;
74+
enum interesting match = revs->diffopt.pathspec.nr == 0 ?
75+
all_entries_interesting: entry_not_interesting;
7576
int baselen = base->len;
7677

7778
if (!revs->tree_objects)
@@ -97,12 +98,12 @@ static void process_tree(struct rev_info *revs,
9798
init_tree_desc(&desc, tree->buffer, tree->size);
9899

99100
while (tree_entry(&desc, &entry)) {
100-
if (match != 2) {
101+
if (match != all_entries_interesting) {
101102
match = tree_entry_interesting(&entry, base, 0,
102103
&revs->diffopt.pathspec);
103-
if (match < 0)
104+
if (match == all_entries_not_interesting)
104105
break;
105-
if (match == 0)
106+
if (match == entry_not_interesting)
106107
continue;
107108
}
108109

sha1_file.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1267,7 +1267,8 @@ unsigned long unpack_object_header_buffer(const unsigned char *buf,
12671267
while (c & 0x80) {
12681268
if (len <= used || bitsizeof(long) <= shift) {
12691269
error("bad object header");
1270-
return 0;
1270+
size = used = 0;
1271+
break;
12711272
}
12721273
c = buf[used++];
12731274
size += (c & 0x7f) << shift;

tree-diff.c

Lines changed: 12 additions & 10 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);
@@ -64,14 +64,14 @@ static int compare_tree_entry(struct tree_desc *t1, struct tree_desc *t2,
6464
static void show_tree(struct diff_options *opt, const char *prefix,
6565
struct tree_desc *desc, struct strbuf *base)
6666
{
67-
int match = 0;
67+
enum interesting match = entry_not_interesting;
6868
for (; desc->size; update_tree_entry(desc)) {
69-
if (match != 2) {
69+
if (match != all_entries_interesting) {
7070
match = tree_entry_interesting(&desc->entry, base, 0,
7171
&opt->pathspec);
72-
if (match < 0)
72+
if (match == all_entries_not_interesting)
7373
break;
74-
if (match == 0)
74+
if (match == entry_not_interesting)
7575
continue;
7676
}
7777
show_entry(opt, prefix, desc, 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);
@@ -114,12 +114,13 @@ static void show_entry(struct diff_options *opt, const char *prefix,
114114
}
115115

116116
static void skip_uninteresting(struct tree_desc *t, struct strbuf *base,
117-
struct diff_options *opt, int *match)
117+
struct diff_options *opt,
118+
enum interesting *match)
118119
{
119120
while (t->size) {
120121
*match = tree_entry_interesting(&t->entry, base, 0, &opt->pathspec);
121122
if (*match) {
122-
if (*match < 0)
123+
if (*match == all_entries_not_interesting)
123124
t->size = 0;
124125
break;
125126
}
@@ -132,7 +133,8 @@ int diff_tree(struct tree_desc *t1, struct tree_desc *t2,
132133
{
133134
struct strbuf base;
134135
int baselen = strlen(base_str);
135-
int t1_match = 0, t2_match = 0;
136+
enum interesting t1_match = entry_not_interesting;
137+
enum interesting t2_match = entry_not_interesting;
136138

137139
/* Enable recursion indefinitely */
138140
opt->pathspec.recursive = DIFF_OPT_TST(opt, RECURSIVE);

tree-walk.c

Lines changed: 38 additions & 37 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);
@@ -465,7 +465,6 @@ int get_tree_entry(const unsigned char *tree_sha1, const char *name, unsigned ch
465465
int retval;
466466
void *tree;
467467
unsigned long size;
468-
struct tree_desc t;
469468
unsigned char root[20];
470469

471470
tree = read_object_with_reference(tree_sha1, tree_type, &size, root);
@@ -478,8 +477,13 @@ int get_tree_entry(const unsigned char *tree_sha1, const char *name, unsigned ch
478477
return 0;
479478
}
480479

481-
init_tree_desc(&t, tree, size);
482-
retval = find_tree_entry(&t, name, sha1, mode);
480+
if (!size) {
481+
retval = -1;
482+
} else {
483+
struct tree_desc t;
484+
init_tree_desc(&t, tree, size);
485+
retval = find_tree_entry(&t, name, sha1, mode);
486+
}
483487
free(tree);
484488
return retval;
485489
}
@@ -573,30 +577,26 @@ static int match_dir_prefix(const char *base,
573577
*
574578
* Pre-condition: either baselen == base_offset (i.e. empty path)
575579
* or base[baselen-1] == '/' (i.e. with trailing slash).
576-
*
577-
* Return:
578-
* - 2 for "yes, and all subsequent entries will be"
579-
* - 1 for yes
580-
* - zero for no
581-
* - negative for "no, and no subsequent entries will be either"
582580
*/
583-
int tree_entry_interesting(const struct name_entry *entry,
584-
struct strbuf *base, int base_offset,
585-
const struct pathspec *ps)
581+
enum interesting tree_entry_interesting(const struct name_entry *entry,
582+
struct strbuf *base, int base_offset,
583+
const struct pathspec *ps)
586584
{
587585
int i;
588586
int pathlen, baselen = base->len - base_offset;
589-
int never_interesting = ps->has_wildcard ? 0 : -1;
587+
int never_interesting = ps->has_wildcard ?
588+
entry_not_interesting : all_entries_not_interesting;
590589

591590
if (!ps->nr) {
592591
if (!ps->recursive || ps->max_depth == -1)
593-
return 2;
594-
return !!within_depth(base->buf + base_offset, baselen,
595-
!!S_ISDIR(entry->mode),
596-
ps->max_depth);
592+
return all_entries_interesting;
593+
return within_depth(base->buf + base_offset, baselen,
594+
!!S_ISDIR(entry->mode),
595+
ps->max_depth) ?
596+
entry_interesting : entry_not_interesting;
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;
@@ -610,38 +610,39 @@ int tree_entry_interesting(const struct name_entry *entry,
610610
goto match_wildcards;
611611

612612
if (!ps->recursive || ps->max_depth == -1)
613-
return 2;
613+
return all_entries_interesting;
614614

615-
return !!within_depth(base_str + matchlen + 1,
616-
baselen - matchlen - 1,
617-
!!S_ISDIR(entry->mode),
618-
ps->max_depth);
615+
return within_depth(base_str + matchlen + 1,
616+
baselen - matchlen - 1,
617+
!!S_ISDIR(entry->mode),
618+
ps->max_depth) ?
619+
entry_interesting : entry_not_interesting;
619620
}
620621

621622
/* Either there must be no base, or the base must match. */
622623
if (baselen == 0 || !strncmp(base_str, match, baselen)) {
623624
if (match_entry(entry, pathlen,
624625
match + baselen, matchlen - baselen,
625626
&never_interesting))
626-
return 1;
627+
return entry_interesting;
627628

628-
if (ps->items[i].use_wildcard) {
629+
if (item->use_wildcard) {
629630
if (!fnmatch(match + baselen, entry->path, 0))
630-
return 1;
631+
return entry_interesting;
631632

632633
/*
633634
* Match all directories. We'll try to
634635
* match files later on.
635636
*/
636637
if (ps->recursive && S_ISDIR(entry->mode))
637-
return 1;
638+
return entry_interesting;
638639
}
639640

640641
continue;
641642
}
642643

643644
match_wildcards:
644-
if (!ps->items[i].use_wildcard)
645+
if (!item->use_wildcard)
645646
continue;
646647

647648
/*
@@ -653,7 +654,7 @@ int tree_entry_interesting(const struct name_entry *entry,
653654

654655
if (!fnmatch(match, base->buf + base_offset, 0)) {
655656
strbuf_setlen(base, base_offset + baselen);
656-
return 1;
657+
return entry_interesting;
657658
}
658659
strbuf_setlen(base, base_offset + baselen);
659660

@@ -662,7 +663,7 @@ int tree_entry_interesting(const struct name_entry *entry,
662663
* later on.
663664
*/
664665
if (ps->recursive && S_ISDIR(entry->mode))
665-
return 1;
666+
return entry_interesting;
666667
}
667668
return never_interesting; /* No matches */
668669
}

0 commit comments

Comments
 (0)