tech-net archive

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

Re: using the interfaces in ctype.h



On Wed, Apr 16, 2008 at 10:24:41PM -0400, Greg A. Woods; Planix, Inc. wrote:
> 
>  On 16-Apr-08, at 6:30 PM, Christos Zoulas wrote:
> > - cast ctype arguments to unsigned char and not int, otherwise you silence
> > the warning but you can still get a core-dump.
> 
> 
>  If you're talking about the is*() and to*() APIs from libc then that doesn't 
>  seem to make any sense for a standards compatible program.
> 
>  The definitions in all of the standards, an in the NetBSD manual pages, all 
>  request that the parameters for these functions are to be passed as (int).
> 
>  Unfortunately even various NetBSD architectures, at least for some releases, 
>  are inconsistent in how these interfaces are defined and what warnings are 
>  given for various usages.  I've had the best luck getting the least warnings 
>  on a wide variety of platforms by always casting to (int) too.

These functions accept int only because they need to handle (-1) argument,
i.e. possible result of getc(3). I.e. valid character code range is
from -1 to 256.

If char is signed (it is on NetBSD/i386 IIRC), by casting to int
you get negative number and the array access done by is*() macros
causes coredump (or overwrites some memory).

You must cast argument of is*() to (unsigned char) always first. You can
do (int)(unsigned char) cast if just (unsigned char) gives you a warning.

Jaromir
-- 
Jaromir Dolecek <jdolecek%NetBSD.org@localhost>            http://www.NetBSD.cz/
-=- We can walk our road together if our goals are all the same;     -=-
-=- We can run alone and free if we pursue a different aim.          -=-


Home | Main Index | Thread Index | Old Index