tinyexpr icon indicating copy to clipboard operation
tinyexpr copied to clipboard

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

Open brazoayeye opened this issue 2 years ago • 2 comments

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)

brazoayeye avatar Aug 29 '23 14:08 brazoayeye

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.

ManuTester avatar Dec 06 '23 22:12 ManuTester

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

codeplea avatar Dec 07 '23 01:12 codeplea