[Bug c/78155] missing warning on invalid usage of functions/macros from <ctype.h> (isalpha et al.)
egallager at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Wed Jun 8 17:39:23 GMT 2022
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78155
--- Comment #8 from Eric Gallager <egallager at gcc dot gnu.org> ---
(In reply to Martin Sebor from comment #4)
> I don't really see what existing warning this might fall under, except
> perhaps -Wchar-subscripts because isalpha and friend use the argument as an
> index into an array of 257 characters, but that seems like a stretch.
>
> I think maybe adding a more general warning option, say something like
> -Wargument-range, and using it to diagnose all such problems, might be the
> way to go. To generalize the solution I would even consider adding a new
> function attribute, let's call it range, to specify the range of valid
> values of a function argument. Then isalpha (or any other such function)
> could be declared like so:
>
> __attribute__ ((range (/* position = */1, -1, UCHAR_MAX)))
> int isalpha (int);
>
> GCC would then check every call to the function to see if its argument is in
> the expected range and, if not, issue a warning. The attribute could even
> be applied multiple times to specify disjoint ranges. Position zero could
> denote the return value so that toupper could be declared like so
>
> __attribute__ ((range (/* returns = */ 0, -1, UCHAR_MAX),
> range (/* position = */ 1, -1, UCHAR_MAX)))
> int toupper (int);
There's been an attempt to add an attribute like this recently on the mailing
lists: https://gcc.gnu.org/pipermail/gcc/2022-June/238819.html
More information about the Gcc-bugs
mailing list