Current-Users archive

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

Re: ctype and gcc9

Patrick Welche <> writes:

> Since gcc9, essentially every ctype using piece of software fails with
>    error: array subscript has type 'char' [-Werror=char-subscripts]
> which prompts a style question: cast every argument of every call to
> a ctype function in every piece of software to (unsigned char), or
> -Wno-error=char-subscripts], or something else?

There were earlier warnings that were similar.  This is tricky business!

POSIX says (quoting C99, which is harder to give a URL to):

which says that these are functions.  However in NetBSD they are macros.
I have seen arguments that the macro implementation is legitimate.  And,
I think a true function would have the same problem, just showing up
differently in terms of warnings (promoting char to int in a function
call, when the function has UB for many of those values -- a warning
less likely to be issued).

The caller must provide an int which is representable as unsigned char
(or EOF), says the definition, if you read it straightforwardly without
trying to be a language lawyer thinking about macros.

If you pass a char to a function that takes int, and char is a signed
type, and the value is negative, there is sign extension, and thus
undefined behavior when using a ctype function.

So, it's more than a style question; code that passes char to ctype
funtions is actually wrong, and yes it should all be fixed.

Attachment: signature.asc
Description: PGP signature

Home | Main Index | Thread Index | Old Index