Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importing into gcc gives "error: array subscript has type 'char'" #105

Open
brazoayeye opened this issue Aug 29, 2023 · 2 comments
Open

Comments

@brazoayeye
Copy link

I'm using ESP-IDF and i'm importing this into a c++ project based on ESP-IDF 5.2.

During compilation I have many errors:
"error: array subscript has type 'char'"

The compilation succeded if I change the code to

#define isalpha(__c) (__ctype_lookup((unsigned char)__c)&(_U|_L))
and
#define isdigit(__c) (__ctype_lookup((unsigned char)__c)&_N)

@ManuTester
Copy link

This is a bug. isalpha and isdigit take an int, but they expect a value of 0..255 (or EOF) and not -128..127
One is supposed to cast a char into unsigned char before putting it into isalpha and the like. This was not fatal before, because -128...-1 are neither alphas nor digits so the function returned 0 for numbers < 0 anyway (EOF may be -1).
But now nano libraries seem to leave out the EOF support and just use the value as index to an array, which can be pretty bad with negative numbers.
If you do a man isalpha, you will see the NOTES:
NOTES
The standards require that the argument c for these functions is either EOF or a value that is representable in the type unsigned char. If the argument c is of type char, it must be cast
to unsigned char, as in the following example:

       char c;
       ...
       res = toupper((unsigned char) c);

   This is necessary because char may be the equivalent of signed char, in which case a byte where the top bit is set would be sign extended when converting to int, yielding a value  that  is
   outside the range of unsigned char.

   The details of what characters belong to which class depend on the locale.  For example, isupper() will not recognize an A-umlaut (Ä) as an uppercase letter in the default C locale.

@codeplea
Copy link
Owner

codeplea commented Dec 7, 2023

Thanks for the detailed explanation. I will try to fix this in the next update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants