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 <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Dec 27, 2024 at 11:46 UTC 5e7fe8a7b89a07d8c3ab298ac69bc33f6ba88b47
5 files changed +11 -12
bisect.c
+1 -1
@@ -855,7 +855,7 @@ static void handle_skipped_merge_base(const struct object_id *mb)
855 * for early success, this will be converted back to 0 in
856 * check_good_are_ancestors_of_bad().
857 */
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)
859 {
860 enum bisect_error res = BISECT_OK;
861 struct commit_list *result = NULL;
builtin/merge-base.c
+2 -2
@@ -8,7 +8,7 @@
8 #include "parse-options.h"
9 #include "commit-reach.h"
10
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)
12 {
13 struct commit_list *result = NULL, *r;
14
@@ -149,7 +149,7 @@ int cmd_merge_base(int argc,
149 struct repository *repo UNUSED)
150 {
151 struct commit **rev;
152 - int rev_nr = 0;
152 + size_t rev_nr = 0;
153 int show_all = 0;
154 int cmdmode = 0;
155 int ret;
commit-reach.c
+3 -4
@@ -1,5 +1,4 @@
1 #define USE_THE_REPOSITORY_VARIABLE
2 -#define DISABLE_SIGN_COMPARE_WARNINGS
2
3 #include "git-compat-util.h"
4 #include "commit.h"
@@ -421,7 +420,7 @@ static int remove_redundant(struct repository *r, struct commit **array,
420
421 static int get_merge_bases_many_0(struct repository *r,
422 struct commit *one,
424 - int n,
423 + size_t n,
424 struct commit **twos,
425 int cleanup,
426 struct commit_list **result)
@@ -469,7 +468,7 @@ static int get_merge_bases_many_0(struct repository *r,
468
469 int repo_get_merge_bases_many(struct repository *r,
470 struct commit *one,
472 - int n,
471 + size_t n,
472 struct commit **twos,
473 struct commit_list **result)
474 {
@@ -478,7 +477,7 @@ int repo_get_merge_bases_many(struct repository *r,
477
478 int repo_get_merge_bases_many_dirty(struct repository *r,
479 struct commit *one,
481 - int n,
480 + size_t n,
481 struct commit **twos,
482 struct commit_list **result)
483 {
commit-reach.h
+2 -2
@@ -14,12 +14,12 @@ int repo_get_merge_bases(struct repository *r,
14 struct commit *rev2,
15 struct commit_list **result);
16 int repo_get_merge_bases_many(struct repository *r,
17 - struct commit *one, int n,
17 + struct commit *one, size_t n,
18 struct commit **twos,
19 struct commit_list **result);
20 /* To be used only when object flags after this call no longer matter */
21 int repo_get_merge_bases_many_dirty(struct repository *r,
22 - struct commit *one, int n,
22 + struct commit *one, size_t n,
23 struct commit **twos,
24 struct commit_list **result);
25
t/helper/test-reach.c
+3 -3
@@ -35,7 +35,7 @@ int cmd__reach(int ac, const char **av)
35 struct commit_list *X, *Y;
36 struct object_array X_obj = OBJECT_ARRAY_INIT;
37 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;
39 struct strbuf buf = STRBUF_INIT;
40 struct repository *r = the_repository;
41
@@ -157,7 +157,7 @@ int cmd__reach(int ac, const char **av)
157 clear_contains_cache(&cache);
158 } else if (!strcmp(av[1], "get_reachable_subset")) {
159 const int reachable_flag = 1;
160 - int i, count = 0;
160 + int count = 0;
161 struct commit_list *current;
162 struct commit_list *list = get_reachable_subset(X_array, X_nr,
163 Y_array, Y_nr,
@@ -169,7 +169,7 @@ int cmd__reach(int ac, const char **av)
169 oid_to_hex(&list->item->object.oid));
170 count++;
171 }
172 - for (i = 0; i < Y_nr; i++) {
172 + for (size_t i = 0; i < Y_nr; i++) {
173 if (Y_array[i]->object.flags & reachable_flag)
174 count--;
175 }