Skip to content
  • Taylor R Campbell's avatar
    Avoid misuse of ctype(3) functions · b1ccd521
    Taylor R Campbell authored
    The ctype(3) character classification and mapping functions have a
    peculiarly limited definition (C11, Sec. 7.4 `Character handling
    <ctype.h>', p. 200):
    
    	`The header <ctype.h> declares several functions useful for
    	 classifying and mapping characters.  In all cases the
    	 argument is an int, the value of which shall be
    	 representable as an unsigned char or shall equal the value
    	 of the macro EOF.  If the argument has any other value, the
    	 behavior is undefined.'
    
    In other words, in the most common case of 8-bit char and EOF = -1,
    the domain of the 257 allowed arguments is:
    
    	-1, 0, 1, 2, ..., 254, 255
    
    The ctype(3) functions are designed for use with stdio functions like
    getchar and fgetc which return int values in the same domain.
    
    In an ABI where char is signed (e.g., x86 SysV ABI used by most
    Unixish operating systems), passing an argument of type char as is
    can go wrong in two ways:
    
    1. The value of a non-EOF input octet interpreted as `char' may
       coincide, as a...
    b1ccd521