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

--- Comment #15 from Roland Illig <roland.illig at gmx dot de> ---
(In reply to Steve Kargl from comment #9)
> I'm still trying to understand how an
> option names -Werror=char-subscripts could trigger an
> error.  There are no subscripts.

There _are_ subscripts, just look at the precompiled C translation unit.

$ printf '%s\n' '#include <ctype.h>' 'isspace(ch)' | gcc -E -

On Cygwin, the output ends with:

    ((((__locale_ctype_ptr ()) + sizeof(""[ch]))[(int)(ch)])&010)

The expression `sizeof(""[ch])` has two purposes. The first is to add 1 to the
array index (since EOF is -1 in that implementation), the second is to trigger
the GCC diagnostic, to prevent the undefined behavior. That's why all C
programmers who know the <ctype.h> functions cast the argument from `char` to
`unsigned char`, to be absolutely sure that there is no undefined behavior, no
matter what value the argument has.

On NetBSD, the pattern is similar:

    ((int)((_ctype_tab_ + 1)[(ch)] & 0x0040))

Here, the argument is directly used as an array subscript, without casting it
first, with the same effect of triggering the GCC diagnostic.

Reply via email to