Skip to content

Commit a416585

Browse files
peffgitster
authored andcommitted
silence gcc array-bounds warning
In shorten_unambiguous_ref, we build and cache a reverse-map of the rev-parse rules like this: static char **scanf_fmts; static int nr_rules; if (!nr_rules) { for (; ref_rev_parse_rules[nr_rules]; nr_rules++) ... generate scanf_fmts ... } where ref_rev_parse_rules is terminated with a NULL pointer. Compiling with "gcc -O2 -Wall" does not cause any problems, but compiling with "-O3 -Wall" generates: $ make CFLAGS='-O3 -Wall' refs.o refs.c: In function ‘shorten_unambiguous_ref’: refs.c:3379:29: warning: array subscript is above array bounds [-Warray-bounds] for (; ref_rev_parse_rules[nr_rules]; nr_rules++) Curiously, we can silence this by explicitly nr_rules to 0 in the beginning of the loop, even though the compiler should be able to tell that we follow this code path only when nr_rules is already 0. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7c2b302 commit a416585

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

refs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1688,7 +1688,7 @@ char *shorten_unambiguous_ref(const char *ref)
16881688
size_t total_len = 0;
16891689

16901690
/* the rule list is NULL terminated, count them first */
1691-
for (; ref_rev_parse_rules[nr_rules]; nr_rules++)
1691+
for (nr_rules = 0; ref_rev_parse_rules[nr_rules]; nr_rules++)
16921692
/* no +1 because strlen("%s") < strlen("%.*s") */
16931693
total_len += strlen(ref_rev_parse_rules[nr_rules]);
16941694

0 commit comments

Comments
 (0)