This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug middle-end/83312] New: [8 regression] bogus -Warray-bounds warning


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=83312

            Bug ID: 83312
           Summary: [8 regression] bogus -Warray-bounds warning
           Product: gcc
           Version: 8.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: arnd at linaro dot org
  Target Milestone: ---

I got an odd -Warray-bounds warning for Linux kernel code, original source code
is
https://git.kernel.org/pub/scm/linux/kernel/git/next/linux-next-history.git/tree/drivers/staging/lustre/lustre/ptlrpc/ptlrpcd.c?h=next-20171207#n492,
partially reduced test case follows:

struct ptlrpcd_ctl {
  char pc_name[20];
};
struct ptlrpcd {
  struct ptlrpcd_ctl pd_threads[6];
};
struct ptlrpcd *ptlrpcd_init_pd;
static void ptlrpcd_ctl_init(struct ptlrpcd_ctl *pc, int index) {
  if (index < 0)
    __builtin_snprintf(pc->pc_name, sizeof(pc->pc_name), "ptlrpcd_rcv");
  else
    __builtin_snprintf(pc->pc_name, sizeof(pc->pc_name), "ptlrpcd_%d", index);
}
int ptlrpcd_init_ncpts;
static int ptlrpcd_init(int nthreads) {
  int j;
  if (ptlrpcd_init_ncpts) {
    ptlrpcd_ctl_init(&ptlrpcd_init_pd->pd_threads[0], -1);
    for (j = 1; j < nthreads; j++)
      ptlrpcd_ctl_init(&ptlrpcd_init_pd->pd_threads[j], j);
  }
  return 0;
}
int ptlrpcd_init_groupsize;
void ptlrpcd_addref(void) {
    ptlrpcd_init(ptlrpcd_init_groupsize);
}

$ x86_64-linux-gcc-8.0.0 --version
x86_64-linux-gcc-8.0.0 (GCC) 8.0.0 20171201 (experimental)
$ x86_64-linux-gcc-8.0.0   -c -O2 -Wall   -Werror ptlrpcd.i
ptlrpcd.i: In function 'ptlrpcd_addref':
ptlrpcd.i:10:26: error: array subscript -1 is below array bounds of 'struct
ptlrpcd_ctl[6]' [-Werror=array-bounds]
     __builtin_snprintf(pc->pc_name, sizeof(pc->pc_name), "ptlrpcd_rcv");

Reducing it further leads to nonsensical code with a valid warning that
couldn't really happen in the original code:

struct ptlrpcd_ctl { char pc_name[6]; };
struct { struct ptlrpcd_ctl pd_threads[0]; } b;
int a;
int fn1(char *, long, char *, ...);
void fn2(struct ptlrpcd_ctl *p1, int p2) { if (p2 < 0) fn1(p1->pc_name,
sizeof(p1), ""); }
void fn3() { fn2(&b.pd_threads[a], a); }

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]