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

            Bug ID: 94247
           Summary: Wrong char-subscripts warning for limited-range index
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: roland.illig at gmx dot de
  Target Milestone: ---

#define MAXID 20
static const char shft[MAXID] = {1,2,3,4,5,6,7,1,2,3,4,5,6,7,1,2,3,4,5,6};

int hashstr(const char *s) {
  char c;
  char k = 0;
  unsigned int sum = 0;

  while( (c = *s++) != '\0' && k < MAXID-1 ) {
    sum += c + (c<<shft[k++]);
  }
  return (int)(sum >> 1);
}

The above program is a slight variation of the OpenJDK code in
src/hotspot/share/libadt/dict.cpp. It uses a char for indexing an array, which
triggers this warning:

dict.cpp: In function ‘int hashstr(const char*)’:
dict.cpp:10:28: warning: array subscript has type ‘char’ [-Wchar-subscripts]
     sum += c + (c<<shft[k++]);

At optimization levels 1 and beyond, the possible range for k is determined
correctly, and the compiler generates the same code, no matter if k has type
char or unsigned int or any other integer type.

The warning is a false positive, and the compiler already knows this. Therefore
the warning should not be generated.

Reply via email to