This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/16302] New: gcc fails to warn about some common logic errors
- From: "trt at acm dot org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 30 Jun 2004 19:33:07 -0000
- Subject: [Bug c/16302] New: gcc fails to warn about some common logic errors
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
A "gcc -Wextra -O" misses the 4 bugs in this test program:
int
main (int argc, char *argv[])
{
if (argc != 1 || argc != 2) return 1;
if (argc < 0 && argc > 10) return 1;
if (argc || !argc) return 1;
if (argc && !argc) return 1;
return 0;
}
With a tweak to fold-const.c (below), they will be caught:
t.c:4: warning: `or' of collectively exhaustive tests is always 1
t.c:5: warning: `and' of mutually exclusive tests is always 0
t.c:6: warning: `or' of collectively exhaustive tests is always 1
t.c:7: warning: `and' of mutually exclusive tests is always 0
A bootstrap of gcc-3.5-20040627 reports such a warning in fold-const.c itself,
the patch below includes the obvious fix.
====
Comment: The -O is needed due to this code in fold-const.c:
/* We only do these simplifications if we are optimizing. */
if (!optimize)
return t;
This is probably from when fold-const.c was much simpler,
but I think is no longer appropriate and recommend it be deleted.
====
*** fold-const.c.orig Sun Jun 27 11:23:44 2004
--- fold-const.c Wed Jun 30 12:43:39 2004
***************
*** 4377,4379 ****
in_p, low, high))))
! return or_op ? invert_truthvalue (tem) : tem;
--- 4377,4388 ----
in_p, low, high))))
! {
! if (TREE_CODE (tem) == INTEGER_CST && extra_warnings)
! {
! if (or_op)
! warning ("`or' of collectively exhaustive tests is always 1");
! else
! warning ("`and' of mutually exclusive tests is always 0");
! }
! return or_op ? invert_truthvalue (tem) : tem;
! }
***************
*** 9998,10000 ****
|| (TREE_CODE (op0) == REAL_CST
! && TREE_CODE (op0) != REAL_CST))
{
--- 10007,10009 ----
|| (TREE_CODE (op0) == REAL_CST
! && TREE_CODE (op1) != REAL_CST))
{
--
Summary: gcc fails to warn about some common logic errors
Product: gcc
Version: 3.5.0
Status: UNCONFIRMED
Severity: normal
Priority: P2
Component: c
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: trt at acm dot org
CC: gcc-bugs at gcc dot gnu dot org
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16302