]> git.scripts.mit.edu Git - git.git/commitdiff
silence gcc array-bounds warning
authorJeff King <peff@peff.net>
Thu, 24 Oct 2013 08:45:13 +0000 (04:45 -0400)
committerJunio C Hamano <gitster@pobox.com>
Thu, 24 Oct 2013 22:41:56 +0000 (15:41 -0700)
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 <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
refs.c

diff --git a/refs.c b/refs.c
index 1e5e7b4ad953ca55ffc4b7da439f90fa4715bbb1..e264f7a9f493671db652599172b472b3ef4a00b6 100644 (file)
--- a/refs.c
+++ b/refs.c
@@ -1688,7 +1688,7 @@ char *shorten_unambiguous_ref(const char *ref)
                size_t total_len = 0;
 
                /* the rule list is NULL terminated, count them first */
-               for (; ref_rev_parse_rules[nr_rules]; nr_rules++)
+               for (nr_rules = 0; ref_rev_parse_rules[nr_rules]; nr_rules++)
                        /* no +1 because strlen("%s") < strlen("%.*s") */
                        total_len += strlen(ref_rev_parse_rules[nr_rules]);