Skip to content

Commit 03e8004

Browse files
committed
Merge branch 'ma/bisect-leakfix' into maint
Leak fixes. * ma/bisect-leakfix: bisect: fix memory leak when returning best element bisect: fix off-by-one error in `best_bisection_sorted()` bisect: fix memory leak in `find_bisection()` bisect: change calling-convention of `find_bisection()`
2 parents df481b9 + f4e45cb commit 03e8004

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

bisect.c

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,11 @@ static struct commit_list *best_bisection_sorted(struct commit_list *list, int n
226226
add_name_decoration(DECORATION_NONE, buf.buf, obj);
227227

228228
p->item = array[i].commit;
229-
p = p->next;
229+
if (i < cnt - 1)
230+
p = p->next;
230231
}
231-
if (p)
232-
p->next = NULL;
232+
free_commit_list(p->next);
233+
p->next = NULL;
233234
strbuf_release(&buf);
234235
free(array);
235236
return list;
@@ -360,28 +361,29 @@ static struct commit_list *do_find_bisection(struct commit_list *list,
360361
return best_bisection_sorted(list, nr);
361362
}
362363

363-
struct commit_list *find_bisection(struct commit_list *list,
364-
int *reaches, int *all,
365-
int find_all)
364+
void find_bisection(struct commit_list **commit_list, int *reaches,
365+
int *all, int find_all)
366366
{
367367
int nr, on_list;
368-
struct commit_list *p, *best, *next, *last;
368+
struct commit_list *list, *p, *best, *next, *last;
369369
int *weights;
370370

371-
show_list("bisection 2 entry", 0, 0, list);
371+
show_list("bisection 2 entry", 0, 0, *commit_list);
372372

373373
/*
374374
* Count the number of total and tree-changing items on the
375375
* list, while reversing the list.
376376
*/
377-
for (nr = on_list = 0, last = NULL, p = list;
377+
for (nr = on_list = 0, last = NULL, p = *commit_list;
378378
p;
379379
p = next) {
380380
unsigned flags = p->item->object.flags;
381381

382382
next = p->next;
383-
if (flags & UNINTERESTING)
383+
if (flags & UNINTERESTING) {
384+
free(p);
384385
continue;
386+
}
385387
p->next = last;
386388
last = p;
387389
if (!(flags & TREESAME))
@@ -397,12 +399,16 @@ struct commit_list *find_bisection(struct commit_list *list,
397399
/* Do the real work of finding bisection commit. */
398400
best = do_find_bisection(list, nr, weights, find_all);
399401
if (best) {
400-
if (!find_all)
402+
if (!find_all) {
403+
list->item = best->item;
404+
free_commit_list(list->next);
405+
best = list;
401406
best->next = NULL;
407+
}
402408
*reaches = weight(best);
403409
}
404410
free(weights);
405-
return best;
411+
*commit_list = best;
406412
}
407413

408414
static int register_ref(const char *refname, const struct object_id *oid,
@@ -954,8 +960,7 @@ int bisect_next_all(const char *prefix, int no_checkout)
954960

955961
bisect_common(&revs);
956962

957-
revs.commits = find_bisection(revs.commits, &reaches, &all,
958-
!!skipped_revs.nr);
963+
find_bisection(&revs.commits, &reaches, &all, !!skipped_revs.nr);
959964
revs.commits = managed_skipped(revs.commits, &tried);
960965

961966
if (!revs.commits) {

bisect.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
#ifndef BISECT_H
22
#define BISECT_H
33

4-
extern struct commit_list *find_bisection(struct commit_list *list,
5-
int *reaches, int *all,
6-
int find_all);
4+
/*
5+
* Find bisection. If something is found, `reaches` will be the number of
6+
* commits that the best commit reaches. `all` will be the count of
7+
* non-SAMETREE commits. If nothing is found, `list` will be NULL.
8+
* Otherwise, it will be either all non-SAMETREE commits or the single
9+
* best commit, as chosen by `find_all`.
10+
*/
11+
extern void find_bisection(struct commit_list **list, int *reaches, int *all,
12+
int find_all);
713

814
extern struct commit_list *filter_skipped(struct commit_list *list,
915
struct commit_list **tried,

builtin/rev-list.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,7 @@ int cmd_rev_list(int argc, const char **argv, const char *prefix)
397397
if (bisect_list) {
398398
int reaches = reaches, all = all;
399399

400-
revs.commits = find_bisection(revs.commits, &reaches, &all,
401-
bisect_find_all);
400+
find_bisection(&revs.commits, &reaches, &all, bisect_find_all);
402401

403402
if (bisect_show_vars)
404403
return show_bisect_vars(&info, reaches, all);

0 commit comments

Comments
 (0)