char array subscript in ctype

Ian Jeffray ian at jeffray.co.uk
Sun Dec 30 16:27:59 PST 2001


Peter Naulls wrote:

> In message <20011230102327.GB1117 at ay.free.fr>
>           Vincent Lefevre <vincent at vinc17.org> wrote:
> 
> 
>>On Sat, Dec 29, 2001 at 22:50:47 +0000, Ian Jeffray wrote:
>>
>>>Peter Naulls wrote:
>>>
>>>
>>>>When using libscl and certain combinations of warnings, the ctype
>>>>isspace, etc. macros may give warnings about subscripts being of type
>>>>'char'.
>>>>
>>[snip]
>>
>>>Conversely, those functions are documented as taking int's, so it's
>>>probably better to fix the code you're compiling, rather than have the
>>>compiler/headers hide the issue?  (IE, the headers should do what they
>>>say on the tin - which they do right now)
>>>
>>I completely agree. Using a char as an argument for isspace, etc
>>is a bug, in particular when char's are signed (some characters
>>may have negative values).
>>
> 
> I'm not sure that's a valid argument.  isspace and friends are almost
> always used in the context of unqualified chars (unsigned on ARM, signed
> on x86).   Futhermore, the specification for these functions says that
> the integer passed is interpreted as an unsigned char (or EOF).  The
> cast to unsigned int ensures this.


Not really... the cast "ensures" that you _don't_ know when you've
written some crap code.


> That aside, the real point is that the library headers cause a warning,
> which they really shouldn't.  Although Norcroft's defintions are
> similar, the compiler itself doesn't issue this warning, which is why
> it's not seen.  glibc sets up the functions quite differently so it
> doesn't occur with that.


You're saying that a compiler which doesn't issue a warning about an
incorrect type is preferable to one which does?  That doesn't sound very
sensible to me.

The warning only occurrs in the case where you've supplied a bad
type to the function.  It goes away if you pass an appropriate type.
This is what C functions do.  I hardly think we should be changing the
way the language works just to remove some warnings you don't happen
to like.

Extending your argument to the limit, you may as well put a cast in
to every single function prototype in every .h to avoid warnings.
I'm sure you'll agree that's rubbish.  :-)

Ian.





More information about the gcc mailing list