svn commit: r196752 - head/lib/libc/stdtime
Dag-Erling Smørgrav
des at des.no
Thu Sep 3 07:26:04 UTC 2009
Andrey Chernov <ache at nagual.pp.ru> writes:
> "Simon L. Nielsen" <simon at FreeBSD.org> writes:
> > Acording to the manual page and the C standard book I have,
> > isdigit() takes an int for an argument, so why change this?
> "The value of the argument must be representable as an unsigned char or
> the value of EOF."
>
> Signed char (automatically casted to int in expression even without direct
> (int) cast) passed to any ctype macro potentically may cause a lot of
> problems, such as: incorrect char class detection or even core dump in
> some systems.
>
> This is due to automatic sign extension of 8bit (if present).
To put it more clearly:
- The is*() functions expect an int for the same reason fgetc() returns
one: a char alone can't represent all possible char values *plus*
EOF.
- In most 8-bit locales, there are valid characters outside the 0-127
range. When stored in a signed char, such characters are represented
as negative values. When that signed char is promoted to a signed
int, the negative values remain negative instead of being converted
to the correct positive value.
- This would not be a problem if C distinguished between a character
(unsigned) and a "short short int" (signed).
- The core dump ache refers to can occur with a naïve implementation
that uses a lookup table and checks for EOF, but not for other
negative values.
DES
--
Dag-Erling Smørgrav - des at des.no
More information about the svn-src-head
mailing list