tech-userlevel archive

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

ctype.h and casts [was Re: pkgsrc RC scripts]

> Right, but if it were a real procedure, the explicit cast isn't
> needed, because that promotion is mandatory per the C language spec.

Right.  And it - the cast to int - is not needed here, either, unless
you want to shut up the warning - which is only a warning, and not even
one that's on by default.

> In other words, putting in the cast would be for clarity, not
> correctness; the *defined* behavior is the same.

The cast to int, yes; the behaviour is broken either way.  It's the
cast to unsigned char that is needed for correctness.

>> isspace() is not a procedure call in our implementation, [...]
> Yes, and I'd forgotten that when I posted my note.  File this one
> under "Why macros are not an adequate substitute for inline
> procedures".

Hmm?  Because this one produced a warning that pointed out a bug in the
code, a warning you would not have gotten from a real procedure?

On those grounds, I think the macro is better.

> I do wonder if our code should cast the argument explicitly before
> using it as an array subscript, since (as I noted) that would be done
> automatically were isspace() a real procedure.

No, a real procedure would not cast the argument.  The conversions a
function call does are substantially weaker than a cast's; I think it's
defined to be equivalent to assignment.

Furthermore, this bug - and it is a bug, absent -funsigned-char or
previous testing of the value to make sure it's in range - would not
have been uncovered if the cast had been present.  Again, I prefer the
current behaviour; warnings about buggy code are generally good - even
if, as here, the warning isn't quite the right one - and, while there
are probably cases where the code is not buggy, in my experience they
are substantially outweighed by the buggy cases.

/~\ 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