tech-userlevel archive

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

Re: pkgsrc RC scripts

>>> while (isspace((int)*value))
>> I'm missing why you would bother casting to (int) in the ctype macro
>> arg here.
> with -Wall:
> warning: array subscript has type 'char'

Before adding casts to shut up warnings, you should first understand
the warning.

This warning is warning about a real risk, and casting to int does not
alleviate it.  The real problem here is only partially related to the
one gcc detects and warns about.  The real problem is that isspace is
defined only for argument values in the range of unsigned char, plus
EOF.  It is not defined for negative values, excepting EOF if EOF
happens to be negative.

Thus, passing a signed char to it is wrong, unless you've previously
checked that the value of that signed char is also representible as an
unsigned char.  This means that passing a plain char is equally wrong
unless you can guarantee that chars are unsigned, which isn't the case
on several of our ports.  Casting to int does not fix this; at most, it
shuts up the warning.  The correct cast is to unsigned char.

The gcc warning is actually arising from a separate case where the
possibility of negative values is a risk: array indexing.  Again, it's
possible for the code to be correct, *if* the value of the char is
known to be in the range of valid subscripts, which is rarely so.

/~\ The ASCII                             Mouse
\ / Ribbon Campaign
 X  Against HTML      
/ \ Email!           7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B

Home | Main Index | Thread Index | Old Index