From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 7B8DA3850202; Wed, 8 Jun 2022 17:39:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7B8DA3850202 From: "egallager at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug c/78155] missing warning on invalid usage of functions/macros from (isalpha et al.) Date: Wed, 08 Jun 2022 17:39:23 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c X-Bugzilla-Version: 7.0 X-Bugzilla-Keywords: diagnostic X-Bugzilla-Severity: normal X-Bugzilla-Who: egallager at gcc dot gnu.org X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 X-BeenThere: gcc-bugs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-bugs mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Jun 2022 17:39:23 -0000 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D78155 --- Comment #8 from Eric Gallager --- (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. >=20 > 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: >=20 > __attribute__ ((range (/* position =3D */1, -1, UCHAR_MAX))) > int isalpha (int); >=20 > 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 >=20 > __attribute__ ((range (/* returns =3D */ 0, -1, UCHAR_MAX), > range (/* position =3D */ 1, -1, UCHAR_MAX))) > int toupper (int); There's been an attempt to add an attribute like this recently on the maili= ng lists: https://gcc.gnu.org/pipermail/gcc/2022-June/238819.html=