Skip to content

Commit ede63a1

Browse files
committed
Merge branch 'mh/reflife'
Define memory ownership and lifetime rules for what for-each-ref feeds to its callbacks (in short, "you do not own it, so make a copy if you want to keep it"). * mh/reflife: (25 commits) refs: document the lifetime of the args passed to each_ref_fn register_ref(): make a copy of the bad reference SHA-1 exclude_existing(): set existing_refs.strdup_strings string_list_add_refs_by_glob(): add a comment about memory management string_list_add_one_ref(): rename first parameter to "refname" show_head_ref(): rename first parameter to "refname" show_head_ref(): do not shadow name of argument add_existing(): do not retain a reference to sha1 do_fetch(): clean up existing_refs before exiting do_fetch(): reduce scope of peer_item object_array_entry: fix memory handling of the name field find_first_merges(): remove unnecessary code find_first_merges(): initialize merges variable using initializer fsck: don't put a void*-shaped peg in a char*-shaped hole object_array_remove_duplicates(): rewrite to reduce copying revision: use object_array_filter() in implementation of gc_boundary() object_array: add function object_array_filter() revision: split some overly-long lines cmd_diff(): make it obvious which cases are exclusive of each other cmd_diff(): rename local variable "list" -> "entry" ...
2 parents b27a79d + 4f78c24 commit ede63a1

File tree

15 files changed

+229
-121
lines changed

15 files changed

+229
-121
lines changed

bisect.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
static struct sha1_array good_revs;
1616
static struct sha1_array skipped_revs;
1717

18-
static const unsigned char *current_bad_sha1;
18+
static unsigned char *current_bad_sha1;
1919

2020
static const char *argv_checkout[] = {"checkout", "-q", NULL, "--", NULL};
2121
static const char *argv_show_branch[] = {"show-branch", NULL, NULL};
@@ -404,7 +404,8 @@ static int register_ref(const char *refname, const unsigned char *sha1,
404404
int flags, void *cb_data)
405405
{
406406
if (!strcmp(refname, "bad")) {
407-
current_bad_sha1 = sha1;
407+
current_bad_sha1 = xmalloc(20);
408+
hashcpy(current_bad_sha1, sha1);
408409
} else if (!prefixcmp(refname, "good-")) {
409410
sha1_array_append(&good_revs, sha1);
410411
} else if (!prefixcmp(refname, "skip-")) {

builtin/describe.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ struct commit_name {
4343
unsigned prio:2; /* annotated tag = 2, tag = 1, head = 0 */
4444
unsigned name_checked:1;
4545
unsigned char sha1[20];
46-
const char *path;
46+
char *path;
4747
};
4848
static const char *prio_names[] = {
4949
"head", "lightweight", "annotated",
@@ -127,12 +127,14 @@ static void add_to_known_names(const char *path,
127127
} else {
128128
e->next = NULL;
129129
}
130+
e->path = NULL;
130131
}
131132
e->tag = tag;
132133
e->prio = prio;
133134
e->name_checked = 0;
134135
hashcpy(e->sha1, sha1);
135-
e->path = path;
136+
free(e->path);
137+
e->path = xstrdup(path);
136138
}
137139
}
138140

builtin/diff.c

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -153,21 +153,23 @@ static int builtin_diff_index(struct rev_info *revs,
153153

154154
static int builtin_diff_tree(struct rev_info *revs,
155155
int argc, const char **argv,
156-
struct object_array_entry *ent)
156+
struct object_array_entry *ent0,
157+
struct object_array_entry *ent1)
157158
{
158159
const unsigned char *(sha1[2]);
159160
int swap = 0;
160161

161162
if (argc > 1)
162163
usage(builtin_diff_usage);
163164

164-
/* We saw two trees, ent[0] and ent[1].
165-
* if ent[1] is uninteresting, they are swapped
165+
/*
166+
* We saw two trees, ent0 and ent1. If ent1 is uninteresting,
167+
* swap them.
166168
*/
167-
if (ent[1].item->flags & UNINTERESTING)
169+
if (ent1->item->flags & UNINTERESTING)
168170
swap = 1;
169-
sha1[swap] = ent[0].item->sha1;
170-
sha1[1-swap] = ent[1].item->sha1;
171+
sha1[swap] = ent0->item->sha1;
172+
sha1[1-swap] = ent1->item->sha1;
171173
diff_tree_sha1(sha1[0], sha1[1], "", &revs->diffopt);
172174
log_tree_diff_flush(revs);
173175
return 0;
@@ -251,8 +253,8 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
251253
{
252254
int i;
253255
struct rev_info rev;
254-
struct object_array_entry ent[100];
255-
int ents = 0, blobs = 0, paths = 0;
256+
struct object_array ent = OBJECT_ARRAY_INIT;
257+
int blobs = 0, paths = 0;
256258
const char *path = NULL;
257259
struct blobinfo blob[2];
258260
int nongit;
@@ -337,9 +339,9 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
337339
}
338340

339341
for (i = 0; i < rev.pending.nr; i++) {
340-
struct object_array_entry *list = rev.pending.objects+i;
341-
struct object *obj = list->item;
342-
const char *name = list->name;
342+
struct object_array_entry *entry = &rev.pending.objects[i];
343+
struct object *obj = entry->item;
344+
const char *name = entry->name;
343345
int flags = (obj->flags & UNINTERESTING);
344346
if (!obj->parsed)
345347
obj = parse_object(obj->sha1);
@@ -348,27 +350,21 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
348350
die(_("invalid object '%s' given."), name);
349351
if (obj->type == OBJ_COMMIT)
350352
obj = &((struct commit *)obj)->tree->object;
353+
351354
if (obj->type == OBJ_TREE) {
352-
if (ARRAY_SIZE(ent) <= ents)
353-
die(_("more than %d trees given: '%s'"),
354-
(int) ARRAY_SIZE(ent), name);
355355
obj->flags |= flags;
356-
ent[ents].item = obj;
357-
ent[ents].name = name;
358-
ents++;
359-
continue;
360-
}
361-
if (obj->type == OBJ_BLOB) {
356+
add_object_array(obj, name, &ent);
357+
} else if (obj->type == OBJ_BLOB) {
362358
if (2 <= blobs)
363359
die(_("more than two blobs given: '%s'"), name);
364360
hashcpy(blob[blobs].sha1, obj->sha1);
365361
blob[blobs].name = name;
366-
blob[blobs].mode = list->mode;
362+
blob[blobs].mode = entry->mode;
367363
blobs++;
368-
continue;
369364

365+
} else {
366+
die(_("unhandled object '%s' given."), name);
370367
}
371-
die(_("unhandled object '%s' given."), name);
372368
}
373369
if (rev.prune_data.nr) {
374370
if (!path)
@@ -379,7 +375,7 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
379375
/*
380376
* Now, do the arguments look reasonable?
381377
*/
382-
if (!ents) {
378+
if (!ent.nr) {
383379
switch (blobs) {
384380
case 0:
385381
result = builtin_diff_files(&rev, argc, argv);
@@ -400,23 +396,26 @@ int cmd_diff(int argc, const char **argv, const char *prefix)
400396
}
401397
else if (blobs)
402398
usage(builtin_diff_usage);
403-
else if (ents == 1)
399+
else if (ent.nr == 1)
404400
result = builtin_diff_index(&rev, argc, argv);
405-
else if (ents == 2)
406-
result = builtin_diff_tree(&rev, argc, argv, ent);
407-
else if (ent[0].item->flags & UNINTERESTING) {
401+
else if (ent.nr == 2)
402+
result = builtin_diff_tree(&rev, argc, argv,
403+
&ent.objects[0], &ent.objects[1]);
404+
else if (ent.objects[0].item->flags & UNINTERESTING) {
408405
/*
409406
* diff A...B where there is at least one merge base
410-
* between A and B. We have ent[0] == merge-base,
411-
* ent[ents-2] == A, and ent[ents-1] == B. Show diff
412-
* between the base and B. Note that we pick one
413-
* merge base at random if there are more than one.
407+
* between A and B. We have ent.objects[0] ==
408+
* merge-base, ent.objects[ents-2] == A, and
409+
* ent.objects[ents-1] == B. Show diff between the
410+
* base and B. Note that we pick one merge base at
411+
* random if there are more than one.
414412
*/
415-
ent[1] = ent[ents-1];
416-
result = builtin_diff_tree(&rev, argc, argv, ent);
413+
result = builtin_diff_tree(&rev, argc, argv,
414+
&ent.objects[0],
415+
&ent.objects[ent.nr-1]);
417416
} else
418417
result = builtin_diff_combined(&rev, argc, argv,
419-
ent, ents);
418+
ent.objects, ent.nr);
420419
result = diff_result_code(&rev.diffopt, result);
421420
if (1 < rev.diffopt.skip_stat_unmatch)
422421
refresh_index_quietly();

builtin/fetch.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,8 @@ static int add_existing(const char *refname, const unsigned char *sha1,
600600
{
601601
struct string_list *list = (struct string_list *)cbdata;
602602
struct string_list_item *item = string_list_insert(list, refname);
603-
item->util = (void *)sha1;
603+
item->util = xmalloc(20);
604+
hashcpy(item->util, sha1);
604605
return 0;
605606
}
606607

@@ -619,7 +620,7 @@ static void find_non_local_tags(struct transport *transport,
619620
struct ref **head,
620621
struct ref ***tail)
621622
{
622-
struct string_list existing_refs = STRING_LIST_INIT_NODUP;
623+
struct string_list existing_refs = STRING_LIST_INIT_DUP;
623624
struct string_list remote_refs = STRING_LIST_INIT_NODUP;
624625
const struct ref *ref;
625626
struct string_list_item *item = NULL;
@@ -665,7 +666,7 @@ static void find_non_local_tags(struct transport *transport,
665666
item = string_list_insert(&remote_refs, ref->name);
666667
item->util = (void *)ref->old_sha1;
667668
}
668-
string_list_clear(&existing_refs, 0);
669+
string_list_clear(&existing_refs, 1);
669670

670671
/*
671672
* We may have a final lightweight tag that needs to be
@@ -722,11 +723,11 @@ static int truncate_fetch_head(void)
722723
static int do_fetch(struct transport *transport,
723724
struct refspec *refs, int ref_count)
724725
{
725-
struct string_list existing_refs = STRING_LIST_INIT_NODUP;
726-
struct string_list_item *peer_item = NULL;
726+
struct string_list existing_refs = STRING_LIST_INIT_DUP;
727727
struct ref *ref_map;
728728
struct ref *rm;
729729
int autotags = (transport->remote->fetch_tags == 1);
730+
int retcode = 0;
730731

731732
for_each_ref(add_existing, &existing_refs);
732733

@@ -742,9 +743,9 @@ static int do_fetch(struct transport *transport,
742743

743744
/* if not appending, truncate FETCH_HEAD */
744745
if (!append && !dry_run) {
745-
int errcode = truncate_fetch_head();
746-
if (errcode)
747-
return errcode;
746+
retcode = truncate_fetch_head();
747+
if (retcode)
748+
goto cleanup;
748749
}
749750

750751
ref_map = get_ref_map(transport, refs, ref_count, tags, &autotags);
@@ -753,8 +754,9 @@ static int do_fetch(struct transport *transport,
753754

754755
for (rm = ref_map; rm; rm = rm->next) {
755756
if (rm->peer_ref) {
756-
peer_item = string_list_lookup(&existing_refs,
757-
rm->peer_ref->name);
757+
struct string_list_item *peer_item =
758+
string_list_lookup(&existing_refs,
759+
rm->peer_ref->name);
758760
if (peer_item)
759761
hashcpy(rm->peer_ref->old_sha1,
760762
peer_item->util);
@@ -765,7 +767,8 @@ static int do_fetch(struct transport *transport,
765767
transport_set_option(transport, TRANS_OPT_FOLLOWTAGS, "1");
766768
if (fetch_refs(transport, ref_map)) {
767769
free_refs(ref_map);
768-
return 1;
770+
retcode = 1;
771+
goto cleanup;
769772
}
770773
if (prune) {
771774
/* If --tags was specified, pretend the user gave us the canonical tags refspec */
@@ -808,7 +811,9 @@ static int do_fetch(struct transport *transport,
808811
free_refs(ref_map);
809812
}
810813

811-
return 0;
814+
cleanup:
815+
string_list_clear(&existing_refs, 1);
816+
return retcode;
812817
}
813818

814819
static void set_option(const char *name, const char *value)

builtin/fsck.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ static int mark_object(struct object *obj, int type, void *data)
112112
return 1;
113113
}
114114

115-
add_object_array(obj, (void *) parent, &pending);
115+
add_object_array(obj, NULL, &pending);
116116
return 0;
117117
}
118118

builtin/show-ref.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ static int add_existing(const char *refname, const unsigned char *sha1, int flag
103103
*/
104104
static int exclude_existing(const char *match)
105105
{
106-
static struct string_list existing_refs = STRING_LIST_INIT_NODUP;
106+
static struct string_list existing_refs = STRING_LIST_INIT_DUP;
107107
char buf[1024];
108108
int matchlen = match ? strlen(match) : 0;
109109

bundle.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ int create_bundle(struct bundle_header *header, const char *path,
281281
if (!get_sha1_hex(buf.buf + 1, sha1)) {
282282
struct object *object = parse_object_or_die(sha1, buf.buf);
283283
object->flags |= UNINTERESTING;
284-
add_pending_object(&revs, object, xstrdup(buf.buf));
284+
add_pending_object(&revs, object, buf.buf);
285285
}
286286
} else if (!get_sha1_hex(buf.buf, sha1)) {
287287
struct object *object = parse_object_or_die(sha1, buf.buf);

http-backend.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -410,14 +410,14 @@ static void get_info_refs(char *arg)
410410
strbuf_release(&buf);
411411
}
412412

413-
static int show_head_ref(const char *name, const unsigned char *sha1,
413+
static int show_head_ref(const char *refname, const unsigned char *sha1,
414414
int flag, void *cb_data)
415415
{
416416
struct strbuf *buf = cb_data;
417417

418418
if (flag & REF_ISSYMREF) {
419-
unsigned char sha1[20];
420-
const char *target = resolve_ref_unsafe(name, sha1, 1, NULL);
419+
unsigned char unused[20];
420+
const char *target = resolve_ref_unsafe(refname, unused, 1, NULL);
421421
const char *target_nons = strip_namespace(target);
422422

423423
strbuf_addf(buf, "ref: %s\n", target_nons);

notes.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -918,17 +918,21 @@ int combine_notes_cat_sort_uniq(unsigned char *cur_sha1,
918918
return ret;
919919
}
920920

921-
static int string_list_add_one_ref(const char *path, const unsigned char *sha1,
921+
static int string_list_add_one_ref(const char *refname, const unsigned char *sha1,
922922
int flag, void *cb)
923923
{
924924
struct string_list *refs = cb;
925-
if (!unsorted_string_list_has_string(refs, path))
926-
string_list_append(refs, path);
925+
if (!unsorted_string_list_has_string(refs, refname))
926+
string_list_append(refs, refname);
927927
return 0;
928928
}
929929

930+
/*
931+
* The list argument must have strdup_strings set on it.
932+
*/
930933
void string_list_add_refs_by_glob(struct string_list *list, const char *glob)
931934
{
935+
assert(list->strdup_strings);
932936
if (has_glob_specials(glob)) {
933937
for_each_glob_ref(string_list_add_one_ref, glob, list);
934938
} else {

0 commit comments

Comments
 (0)