From 144e7090045a703c8f5d140474f202ba4f38ac9a Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Fri, 27 Apr 2012 00:26:59 +0200 Subject: bisect: copy filename string obtained from git_path() Prevent the string from being overwritten by other callers of git_path() and friends before we are done using it. Signed-off-by: Michael Haggerty Signed-off-by: Junio C Hamano --- bisect.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'bisect.c') diff --git a/bisect.c b/bisect.c index 6e186e29cc..48acf73391 100644 --- a/bisect.c +++ b/bisect.c @@ -833,7 +833,7 @@ static int check_ancestors(const char *prefix) */ static void check_good_are_ancestors_of_bad(const char *prefix, int no_checkout) { - const char *filename = git_path("BISECT_ANCESTORS_OK"); + char *filename = xstrdup(git_path("BISECT_ANCESTORS_OK")); struct stat st; int fd; @@ -842,11 +842,11 @@ static void check_good_are_ancestors_of_bad(const char *prefix, int no_checkout) /* Check if file BISECT_ANCESTORS_OK exists. */ if (!stat(filename, &st) && S_ISREG(st.st_mode)) - return; + goto done; /* Bisecting with no good rev is ok. */ if (good_revs.nr == 0) - return; + goto done; /* Check if all good revs are ancestor of the bad rev. */ if (check_ancestors(prefix)) @@ -859,6 +859,8 @@ static void check_good_are_ancestors_of_bad(const char *prefix, int no_checkout) filename, strerror(errno)); else close(fd); + done: + free(filename); } /* -- cgit v1.2.1 From d292bfaf356338b41e14e40ce4dbd6b9c8d600ec Mon Sep 17 00:00:00 2001 From: Ramsay Jones Date: Tue, 4 Sep 2012 18:30:21 +0100 Subject: Call git_pathdup() rather than xstrdup(git_path("...")) In addition to updating the two xstrdup(git_path("...")) call sites with git_pathdup(), we also fix a memory leak by freeing the memory allocated to the ADD_EDIT.patch 'file' in the edit_patch() function. Signed-off-by: Ramsay Jones Signed-off-by: Junio C Hamano --- bisect.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bisect.c') diff --git a/bisect.c b/bisect.c index 48acf73391..1aad49b1a6 100644 --- a/bisect.c +++ b/bisect.c @@ -833,7 +833,7 @@ static int check_ancestors(const char *prefix) */ static void check_good_are_ancestors_of_bad(const char *prefix, int no_checkout) { - char *filename = xstrdup(git_path("BISECT_ANCESTORS_OK")); + char *filename = git_pathdup("BISECT_ANCESTORS_OK"); struct stat st; int fd; -- cgit v1.2.1 From c43cb38612192fa45bf7ed981a98bc4ddb86ce75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20Th=C3=A1i=20Ng=E1=BB=8Dc=20Duy?= Date: Fri, 26 Oct 2012 22:53:50 +0700 Subject: Move estimate_bisect_steps to libgit.a MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This function is used by bisect.c, part of libgit.a while estimate_bisect_steps stays in builtin/rev-list.c. Move it to bisect.a so we won't have undefine reference if a standalone program that uses libgit.a happens to pull it in. Signed-off-by: Nguyễn Thái Ngọc Duy Signed-off-by: Jeff King --- bisect.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) (limited to 'bisect.c') diff --git a/bisect.c b/bisect.c index 1aad49b1a6..bd1b7b5dac 100644 --- a/bisect.c +++ b/bisect.c @@ -956,3 +956,41 @@ int bisect_next_all(const char *prefix, int no_checkout) return bisect_checkout(bisect_rev_hex, no_checkout); } +static inline int log2i(int n) +{ + int log2 = 0; + + for (; n > 1; n >>= 1) + log2++; + + return log2; +} + +static inline int exp2i(int n) +{ + return 1 << n; +} + +/* + * Estimate the number of bisect steps left (after the current step) + * + * For any x between 0 included and 2^n excluded, the probability for + * n - 1 steps left looks like: + * + * P(2^n + x) == (2^n - x) / (2^n + x) + * + * and P(2^n + x) < 0.5 means 2^n < 3x + */ +int estimate_bisect_steps(int all) +{ + int n, x, e; + + if (all < 3) + return 0; + + n = log2i(all); + e = exp2i(n); + x = all - e; + + return (e < 3 * x) ? n : n - 1; +} -- cgit v1.2.1 From 7b96d8880252d70c334857f80ef54009133dbaf3 Mon Sep 17 00:00:00 2001 From: John Keeping Date: Wed, 3 Apr 2013 20:17:55 +0100 Subject: bisect: avoid signed integer overflow Signed-off-by: John Keeping Signed-off-by: Junio C Hamano --- bisect.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'bisect.c') diff --git a/bisect.c b/bisect.c index 1aad49b1a6..7f95273e69 100644 --- a/bisect.c +++ b/bisect.c @@ -525,9 +525,9 @@ struct commit_list *filter_skipped(struct commit_list *list, * is increased by one between each call, but that should not matter * for this application. */ -static int get_prn(int count) { +static unsigned get_prn(unsigned count) { count = count * 1103515245 + 12345; - return ((unsigned)(count/65536) % PRN_MODULO); + return (count/65536) % PRN_MODULO; } /* -- cgit v1.2.1 From bf42772e38db8e758aa28a045e8cba88096a9fcc Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Sat, 25 May 2013 11:08:23 +0200 Subject: register_ref(): make a copy of the bad reference SHA-1 The lifetime of the sha1 parameter passed to an each_ref_fn callback is not guaranteed, so make a copy for later use. Signed-off-by: Michael Haggerty Signed-off-by: Junio C Hamano --- bisect.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'bisect.c') diff --git a/bisect.c b/bisect.c index 374d9e24bd..71c19581da 100644 --- a/bisect.c +++ b/bisect.c @@ -15,7 +15,7 @@ static struct sha1_array good_revs; static struct sha1_array skipped_revs; -static const unsigned char *current_bad_sha1; +static unsigned char *current_bad_sha1; static const char *argv_checkout[] = {"checkout", "-q", NULL, "--", NULL}; 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, int flags, void *cb_data) { if (!strcmp(refname, "bad")) { - current_bad_sha1 = sha1; + current_bad_sha1 = xmalloc(20); + hashcpy(current_bad_sha1, sha1); } else if (!prefixcmp(refname, "good-")) { sha1_array_append(&good_revs, sha1); } else if (!prefixcmp(refname, "skip-")) { -- cgit v1.2.1