public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/94247] New: Wrong char-subscripts warning for limited-range index
@ 2020-03-21 10:42 roland.illig at gmx dot de
  2020-03-21 10:48 ` [Bug c/94247] " pinskia at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: roland.illig at gmx dot de @ 2020-03-21 10:42 UTC (permalink / raw)
  To: gcc-bugs

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.

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2020-03-24  0:03 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-21 10:42 [Bug c/94247] New: Wrong char-subscripts warning for limited-range index roland.illig at gmx dot de
2020-03-21 10:48 ` [Bug c/94247] " pinskia at gcc dot gnu.org
2020-03-21 11:09 ` roland.illig at gmx dot de
2020-03-23  7:37 ` rguenth at gcc dot gnu.org
2020-03-23  7:37 ` rguenth at gcc dot gnu.org
2020-03-23 17:32 ` msebor at gcc dot gnu.org
2020-03-23 18:41 ` jakub at gcc dot gnu.org
2020-03-23 23:52 ` msebor at gcc dot gnu.org
2020-03-24  0:03 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).