Possibly buggy use of ctype.h macros.

Takashi Yano takashi.yano@nifty.ne.jp
Wed Jan 3 06:58:39 GMT 2024


On Tue, 02 Jan 2024 19:59:57 -0800
Kaz Kylheku <kaz@kylheku.com> wrote:
> On 2024-01-02 16:11, Takashi Yano via Cygwin wrote:
> > Perhaps, the off-by-one is for EOF as you guess.
> 
> I doubt it. If EOF were out of range of char, it would have to be -129 or less,
> so that -127 would look even more wrong.
> 
> I see EOF is just -1. That value will also be produced by '\xFF', or "\xff"[0], etc.

In systems other than cygwin, ALLOW_NEGATIVE_CTYPE_INDEX might not
be set. In that case, 

 124 const char _ctype_[1 + 256] = {
 125         0,
 126         _CTYPE_DATA_0_127,
 127         _CTYPE_DATA_128_255
 128 };

is used for __CTYPE_PTR.

So, isalpha(EOF) reffers to 0 in line 125 via this trick.

  45 int
  46 isalpha (int c)
  47 {
  48         return(__CTYPE_PTR[c+1] & (_U|_L));
  49 }

In cygwin, both isalpha((char*)0xff) and isalpha(EOF) reffers to
_CTYPE_DATA_128_255[127] in line 89, while isalpha((unsigned char*)0xff)
reffers to _CTYPE_DATA_128_255[127] in line 91. 

  88 char _ctype_b[128 + 256] = {
  89         _CTYPE_DATA_128_255,
  90         _CTYPE_DATA_0_127,
  91         _CTYPE_DATA_128_255
  92 };

-- 
Takashi Yano <takashi.yano@nifty.ne.jp>


More information about the Cygwin mailing list