ctype bug

Jeroen Ruigrok/asmodai asmodai at in-nomine.org
Thu Jul 7 01:01:56 PDT 2005


Matt,

-On [20050706 20:54], Matthew Dillon (dillon at 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.

C90:

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.

[snip]

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]

7.9.1

EOF

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
stream;

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:





More information about the Commits mailing list