git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "Martin Ågren" <martin.agren@gmail.com>
To: git@vger.kernel.org
Cc: Junio C Hamano <gitster@pobox.com>,
	Christian Couder <christian.couder@gmail.com>
Subject: [PATCH v2 3/4] bisect: fix off-by-one error in `best_bisection_sorted()`
Date: Sun,  5 Nov 2017 21:24:30 +0100	[thread overview]
Message-ID: <9e49e4ada1d1ede05b74c63b6e09c8891eba55d5.1509906092.git.martin.agren@gmail.com> (raw)
In-Reply-To: <cover.1509906092.git.martin.agren@gmail.com>

After we have sorted the `cnt`-many commits that we have selected, we
place them into the commit list. We then set `p->next` to NULL, but as
we do so, `p` is already pointing one beyond item number `cnt`. Indeed,
we check whether `p` is NULL before dereferencing it.

This only matters if there are TREESAME-commits. Since they should be
skipped, they are not included in `cnt` and we will hit the situation
where we set `p->next` to NULL. As a result, the list will be one longer
than it should be. The last commit in the list will be one which occurs
earlier, or which shouldn't be included.

Do not update `p` the very last round in the loop. This ensures that
after the loop, `p->next` points to the remainder of the list, and we
can set it to NULL. While we're here, free that remainder to fix a
memory leak.

Signed-off-by: Martin Ågren <martin.agren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 bisect.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/bisect.c b/bisect.c
index 2f4321767..b1941505b 100644
--- a/bisect.c
+++ b/bisect.c
@@ -226,10 +226,11 @@ static struct commit_list *best_bisection_sorted(struct commit_list *list, int n
 		add_name_decoration(DECORATION_NONE, buf.buf, obj);
 
 		p->item = array[i].commit;
-		p = p->next;
+		if (i < cnt - 1)
+			p = p->next;
 	}
-	if (p)
-		p->next = NULL;
+	free_commit_list(p->next);
+	p->next = NULL;
 	strbuf_release(&buf);
 	free(array);
 	return list;
-- 
2.15.0.415.gac1375d7e


  parent reply	other threads:[~2017-11-05 20:25 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-01 20:34 [PATCH 1/3] bisect: fix memory leak and document `find_bisection()` Martin Ågren
2017-11-01 20:34 ` [PATCH 2/3] bisect: fix off-by-one error in `best_bisection_sorted()` Martin Ågren
2017-11-01 20:34 ` [PATCH 3/3] bisect: fix memory leak when returning best element Martin Ågren
2017-11-02  4:54 ` [PATCH 1/3] bisect: fix memory leak and document `find_bisection()` Junio C Hamano
2017-11-02 10:47   ` Martin Ågren
2017-11-05 20:24     ` [PATCH v2 0/4] bisect: assorted leak-fixes Martin Ågren
2017-11-05 20:24       ` [PATCH v2 1/4] bisect: change calling-convention of `find_bisection()` Martin Ågren
2017-11-05 20:24       ` [PATCH v2 2/4] bisect: fix memory leak in `find_bisection()` Martin Ågren
2017-11-05 20:24       ` Martin Ågren [this message]
2017-11-05 20:24       ` [PATCH v2 4/4] bisect: fix memory leak when returning best element Martin Ågren

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9e49e4ada1d1ede05b74c63b6e09c8891eba55d5.1509906092.git.martin.agren@gmail.com \
    --to=martin.agren@gmail.com \
    --cc=christian.couder@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).