public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
* [Bug c/99950] New: Option -Wchar-subscripts leads to wrong fixes
@ 2021-04-07 6:46 roland.illig at gmx dot de
2021-04-07 7:26 ` [Bug c/99950] " egallager at gcc dot gnu.org
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: roland.illig at gmx dot de @ 2021-04-07 6:46 UTC (permalink / raw)
To: gcc-bugs
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.
^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug c/99950] Option -Wchar-subscripts leads to wrong fixes 2021-04-07 6:46 [Bug c/99950] New: Option -Wchar-subscripts leads to wrong fixes roland.illig at gmx dot de @ 2021-04-07 7:26 ` 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 2 siblings, 0 replies; 4+ messages in thread From: egallager at gcc dot gnu.org @ 2021-04-07 7:26 UTC (permalink / raw) To: gcc-bugs https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99950 Eric Gallager <egallager at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- See Also| |https://gcc.gnu.org/bugzill | |a/show_bug.cgi?id=94182 Keywords| |diagnostic CC| |egallager at gcc dot gnu.org --- Comment #1 from Eric Gallager <egallager at gcc dot gnu.org> --- related to bug 94182 ^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug c/99950] Option -Wchar-subscripts leads to wrong fixes 2021-04-07 6:46 [Bug c/99950] New: Option -Wchar-subscripts leads to wrong fixes roland.illig at gmx dot de 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 2 siblings, 0 replies; 4+ messages in thread From: msebor at gcc dot gnu.org @ 2021-04-07 15:47 UTC (permalink / raw) To: gcc-bugs https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99950 Martin Sebor <msebor at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- Ever confirmed|0 |1 CC| |msebor at gcc dot gnu.org See Also| |https://gcc.gnu.org/bugzill | |a/show_bug.cgi?id=78155 Last reconfirmed| |2021-04-07 Severity|normal |enhancement Status|UNCONFIRMED |NEW --- Comment #2 from Martin Sebor <msebor at gcc dot gnu.org> --- Adding a note after the warning suggesting to cast the argument to unsigned char would help. Enabling the warning also when the char argument is cast to a wider type might also be worth considering. See also pr78155 for a related request to enable the warning for calls to the character classification built-ins. $ cat pr99950.c && gcc -O2 -S -Wall -Wchar-subscripts pr99950.c extern int a[255]; int f (char c) { return a[c]; // -Wchar-subscripts } int g (char c) { return a[(int)c]; // same bug, no warning } pr99950.c: In function ‘f’: pr99950.c:5:11: warning: array subscript has type ‘char’ [-Wchar-subscripts] 5 | return a[c]; // -Wchar-subscripts | ^ ^ permalink raw reply [flat|nested] 4+ messages in thread
* [Bug c/99950] Option -Wchar-subscripts leads to wrong fixes when used with functions from <ctype.h> 2021-04-07 6:46 [Bug c/99950] New: Option -Wchar-subscripts leads to wrong fixes roland.illig at gmx dot de 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 ` egallager at gcc dot gnu.org 2 siblings, 0 replies; 4+ messages in thread From: egallager at gcc dot gnu.org @ 2022-04-28 11:59 UTC (permalink / raw) To: gcc-bugs https://gcc.gnu.org/bugzilla/show_bug.cgi?id=99950 Eric Gallager <egallager at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- Summary|Option -Wchar-subscripts |Option -Wchar-subscripts |leads to wrong fixes |leads to wrong fixes when | |used with functions from | |<ctype.h> --- Comment #3 from Eric Gallager <egallager at gcc dot gnu.org> --- retitling for searchability purposes ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2022-04-28 11:59 UTC | newest] Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2021-04-07 6:46 [Bug c/99950] New: Option -Wchar-subscripts leads to wrong fixes roland.illig at gmx dot de 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
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).