Skip to content

Commit 5e7fe8a

Browse files
pks-tgitster
authored andcommitted
commit-reach: use size_t to track indices when computing merge bases
The functions `repo_get_merge_bases_many()` and friends accepts an array of commits as well as a parameter that indicates how large that array is. This parameter is using a signed integer, which leads to a couple of warnings with -Wsign-compare. Refactor the code to use `size_t` to track indices instead and adapt callers accordingly. While most callers are trivial, there are two callers that require a bit more scrutiny: - builtin/merge-base.c:show_merge_base() subtracts `1` from the `rev_nr` before calling `repo_get_merge_bases_many_dirty()`, so if the variable was `0` it would wrap. This code is fine though because its only caller will execute that code only when `argc >= 2`, and it follows that `rev_nr >= 2`, as well. - bisect.ccheck_merge_bases() similarly subtracts `1` from `rev_nr`. Again, there is only a single caller that populates `rev_nr` with `good_revs.nr`. And because a bisection always requires at least one good revision it follws that `rev_nr >= 1`. Mark the file as -Wsign-compare-clean. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 455ac07 commit 5e7fe8a

File tree

5 files changed

+11
-12
lines changed

5 files changed

+11
-12
lines changed

bisect.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -855,7 +855,7 @@ static void handle_skipped_merge_base(const struct object_id *mb)
855855
* for early success, this will be converted back to 0 in
856856
* check_good_are_ancestors_of_bad().
857857
*/
858-
static enum bisect_error check_merge_bases(int rev_nr, struct commit **rev, int no_checkout)
858+
static enum bisect_error check_merge_bases(size_t rev_nr, struct commit **rev, int no_checkout)
859859
{
860860
enum bisect_error res = BISECT_OK;
861861
struct commit_list *result = NULL;

builtin/merge-base.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include "parse-options.h"
99
#include "commit-reach.h"
1010

11-
static int show_merge_base(struct commit **rev, int rev_nr, int show_all)
11+
static int show_merge_base(struct commit **rev, size_t rev_nr, int show_all)
1212
{
1313
struct commit_list *result = NULL, *r;
1414

@@ -149,7 +149,7 @@ int cmd_merge_base(int argc,
149149
struct repository *repo UNUSED)
150150
{
151151
struct commit **rev;
152-
int rev_nr = 0;
152+
size_t rev_nr = 0;
153153
int show_all = 0;
154154
int cmdmode = 0;
155155
int ret;

commit-reach.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#define USE_THE_REPOSITORY_VARIABLE
2-
#define DISABLE_SIGN_COMPARE_WARNINGS
32

43
#include "git-compat-util.h"
54
#include "commit.h"
@@ -421,7 +420,7 @@ static int remove_redundant(struct repository *r, struct commit **array,
421420

422421
static int get_merge_bases_many_0(struct repository *r,
423422
struct commit *one,
424-
int n,
423+
size_t n,
425424
struct commit **twos,
426425
int cleanup,
427426
struct commit_list **result)
@@ -469,7 +468,7 @@ static int get_merge_bases_many_0(struct repository *r,
469468

470469
int repo_get_merge_bases_many(struct repository *r,
471470
struct commit *one,
472-
int n,
471+
size_t n,
473472
struct commit **twos,
474473
struct commit_list **result)
475474
{
@@ -478,7 +477,7 @@ int repo_get_merge_bases_many(struct repository *r,
478477

479478
int repo_get_merge_bases_many_dirty(struct repository *r,
480479
struct commit *one,
481-
int n,
480+
size_t n,
482481
struct commit **twos,
483482
struct commit_list **result)
484483
{

commit-reach.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ int repo_get_merge_bases(struct repository *r,
1414
struct commit *rev2,
1515
struct commit_list **result);
1616
int repo_get_merge_bases_many(struct repository *r,
17-
struct commit *one, int n,
17+
struct commit *one, size_t n,
1818
struct commit **twos,
1919
struct commit_list **result);
2020
/* To be used only when object flags after this call no longer matter */
2121
int repo_get_merge_bases_many_dirty(struct repository *r,
22-
struct commit *one, int n,
22+
struct commit *one, size_t n,
2323
struct commit **twos,
2424
struct commit_list **result);
2525

t/helper/test-reach.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ int cmd__reach(int ac, const char **av)
3535
struct commit_list *X, *Y;
3636
struct object_array X_obj = OBJECT_ARRAY_INIT;
3737
struct commit **X_array, **Y_array;
38-
int X_nr, X_alloc, Y_nr, Y_alloc;
38+
size_t X_nr, X_alloc, Y_nr, Y_alloc;
3939
struct strbuf buf = STRBUF_INIT;
4040
struct repository *r = the_repository;
4141

@@ -157,7 +157,7 @@ int cmd__reach(int ac, const char **av)
157157
clear_contains_cache(&cache);
158158
} else if (!strcmp(av[1], "get_reachable_subset")) {
159159
const int reachable_flag = 1;
160-
int i, count = 0;
160+
int count = 0;
161161
struct commit_list *current;
162162
struct commit_list *list = get_reachable_subset(X_array, X_nr,
163163
Y_array, Y_nr,
@@ -169,7 +169,7 @@ int cmd__reach(int ac, const char **av)
169169
oid_to_hex(&list->item->object.oid));
170170
count++;
171171
}
172-
for (i = 0; i < Y_nr; i++) {
172+
for (size_t i = 0; i < Y_nr; i++) {
173173
if (Y_array[i]->object.flags & reachable_flag)
174174
count--;
175175
}

0 commit comments

Comments
 (0)