cgccnewlib

Why is does char from an array give subscript warning in gcc for isspace in newlib


I´m having troubles understand a compiler warning that we have in our code. The code we have are similar to the one in this example that give the warning (-Wchar-subscripts). We are using ARM gcc 9.2.1 embedded and newlib as standard library.

#include <ctype.h>

int main ()
{
  char str[]="Example string\n";

  int i = isspace(str[1]); // Warning

  char c=str[1];
  i = isspace(c); // No warning

  i = isspace((unsigned char)str[1]); // No warning
}

Example in godbolt

From what I have understood the implementation of isspace can be via array indexing. Hence the warning. But in that case shouldn't both 1 and 2 give the warning? Why is it only the first one that gives the warning?

We solved our code by adding a cast but I'm not really satisfied until I have understood why it helps.


Solution

  • This appears to be the very same bug as discussed in Bugzilla here Bug 95177 - error: array subscript has type char. Basically gcc is inconsistent in its diagnostics and this behavior only appeared in later versions.

    As discussed in that thread, passing char to the ctype.h functions could in theory be a problem in case the char would contain anything unknown. These functions are defined as expecting the input to be something representable as unsigned char, see C17 7.4:

    In all cases the argument is an int, the value of which shall be representable as an unsigned char or shall equal the value of the macro EOF

    Therefore int i = isspace((unsigned char)str[1]); makes the warning go away.

    But the warning is as you can tell inconsistent. Which could possibly be explained by when you are using ctype.h, the compiler could sometimes be picking a macro and sometimes a function.

    Just disable it and regard it as a false positive. According to the Bugzilla log above, this should now have been fixed very recently. gcc (trunk) and gcc >10.3 doesn't give this warning any longer.