tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: using the interfaces in ctype.h



>                 const char *p, *pname;  
> ...
>                         if (*p == '=' || isspace (*p)

This is correct code when, and only when, *p is known to have a
nonnegative value.

This is approximately never the case in such code I've examined in
enough detail to be competent to hold an opinion - except for MD code
targeted at machines where plain char is unsigned.  You said this was
from lex, so I assume that exception does not apply.

> What should I do?

Either cast *p to unsigned char, or cast p to unsigned char *.  Or,
possibly, add "unsigned" to the declaration of p, but that calls for
looking at the rest of the code that uses p.

> (Ignore, as it seems that *p != EOF ?)

If char is signed and EOF is within the range of negative (signed)
chars (our EOF is -1, so the second condition is satisfied) then *p
*can* equal EOF.  Worse, it can equal any of many other negative
values, most of which are outside the domain of isspace() and are thus
liable to cause various misbehaviour.

/~\ The ASCII                           der Mouse
\ / Ribbon Campaign
 X  Against HTML                mouse%rodents-montreal.org@localhost
/ \ Email!           7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B


Home | Main Index | Thread Index | Old Index