public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "roland.illig at gmx dot de" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/99950] New: Option -Wchar-subscripts leads to wrong fixes
Date: Wed, 07 Apr 2021 06:46:18 +0000	[thread overview]
Message-ID: <bug-99950-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99950

            Bug ID: 99950
           Summary: Option -Wchar-subscripts leads to wrong fixes
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: roland.illig at gmx dot de
  Target Milestone: ---

When code calls the functions from <ctype.h> with an expression of type 'char',
GCC and Clang warn:

> array subscript has type char

Since the function prototypes expect an 'int', inexperienced programmers cast
the argument to 'int', which silences the warning:

https://github.com/search?q=%22isspace+int%22&type=code

More experienced programmers who heard something about 'unsigned' cast the
argument to 'unsigned int', which silences the warning as well:

https://github.com/search?q=%22isspace+unsigned+int%22&type=code

To get these programmers to fix their code properly, GCC should provide more
guidance in this area.  As a quick prototype I implemented a check for these
wrong "fixes" in NetBSD's lint, which results in warnings like these:

warning: argument to 'function from <ctype.h>' must be cast to 'unsigned char',
not to 'int' [342]

The implementation is here, including a test:

https://github.com/NetBSD/src/blob/fdd2c09ca474/usr.bin/xlint/lint1/ckctype.c
https://github.com/NetBSD/src/blob/fdd2c09ca474/tests/usr.bin/xlint/lint1/msg_341.c
https://github.com/NetBSD/src/blob/fdd2c09ca474/tests/usr.bin/xlint/lint1/msg_341.exp

Maybe GCC can implement a similar check to prevent these wrong fixes in the
future.

             reply	other threads:[~2021-04-07  6:46 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-07  6:46 roland.illig at gmx dot de [this message]
2021-04-07  7:26 ` [Bug c/99950] " egallager at gcc dot gnu.org
2021-04-07 15:47 ` msebor at gcc dot gnu.org
2022-04-28 11:59 ` [Bug c/99950] Option -Wchar-subscripts leads to wrong fixes when used with functions from <ctype.h> egallager at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-99950-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).