DragonFly BSD
DragonFly commits List (threaded) for 2005-07
[Date Prev][Date Next]  [Thread Prev][Thread Next]  [Date Index][Thread Index]

Re: ctype bug

From: Jeroen Ruigrok/asmodai <asmodai@xxxxxxxxxxxxx>
Date: Thu, 7 Jul 2005 10:01:04 +0200


-On [20050706 20:54], Matthew Dillon (dillon@xxxxxxxxxxxxxxxxxxxx) wrote:
>    Whoops, we have a problem with the ctype code... it is not properly
>    folding negative integers (i.e. when a signed char is passed to 
>    an is*() function).

>    The is*() functions are returning non-zero for all negative numbers,
>    which is wrong.

You seem to be reluctant to accept the standards' wording on this.


7.3 Character handling <ctype.h> (identical for C99, 7.4)

The header <ctype.h> declares several functions useful for testing and
mapping characters. In all cases the argument is an int, the value of which
shall be respresentable as an unsigned char or shall equal the value of the
macro EOF. If the argument has any other value, the behaviour is undefined.

The behaviour of these functions is affected by the current locale. Those
functions that have implementation-defined aspects only when not in the "C"
locale are noted below.


7.3.1 Character testing functions (identical for C99, 7.4.1)

The functions in this subclause return nonzero (true) if and only if the
value of the argument c conforms to that in the description of the function.

[all is*() functions are subclauses of 7.3.1]



which expands to a negative integral constant expression that is returned by
several functions to indicate end-of-file; that is, no more input from a

Comments from PJ Plauger (one of the standards people and author of The
Standard C Library, page 30):

"Use the functions declared in <ctype.h> to test or alter characters that
you read in with fgetc, getc, getchar, all declared in <stdio.h>. If you
store such a value before you test it, declare the data object to have type
int. If you store in any character type instead, you lose information. You
may mistake an end-of-file indication for a valid character. Or you may
convert a valid character code to a negative value, which is unacceptable.

If you generate an argument any other way, be careful. The functions work
properly only for the value EOF, defined in <stdio.h>, and values that type
unsigned char can represent. The characters in the basic C character set
have positive values when represented as type char. Others may not."

SUS, version 2 (same for IEEE Std 1003.1, 2001 and 2004:

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