[Bug c/7776] const char* p = "foo"; if (p == "foo") ... is compiled without warning!

falk.hueffner@student.uni-tuebingen.de gcc-bugzilla@gcc.gnu.org
Sun May 25 01:35:00 GMT 2003


PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=7776



------- Additional Comments From falk.hueffner@student.uni-tuebingen.de  2003-05-25 00:58 -------
Subject: Re: [Bug c/7776] const char* p = "foo"; if (p == "foo") ... is compiled without warning!

"udbz@rz.uni-karlsruhe.de" <gcc-bugzilla@gcc.gnu.org> writes:

> On 24 May 2003, pinskia@physics.uc.edu wrote:
> 
> > confirmed on mainline (20030524).
> > Should there be a warning on comparing a char* to string constant,
> > suggesting strcmp?
> 
> Yes, that was the idea.

Seems like a reasonable idea to me; I don't see any legitimate reasons
for comparisons with string literals. How about something like this?

Index: fold-const.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/fold-const.c,v
retrieving revision 1.252
diff -u -p -r1.252 fold-const.c
--- fold-const.c        23 May 2003 03:46:52 -0000      1.252
+++ fold-const.c        25 May 2003 00:56:19 -0000
@@ -7163,6 +7163,12 @@ fold (expr)
            }
        }
 
+      /* Warn about things like if (str == "foo").  */
+      if ((code == EQ_EXPR || code == NE_EXPR)
+         && TREE_CODE (arg1) == ADDR_EXPR
+         && TREE_CODE (TREE_OPERAND (arg1, 0)) == STRING_CST)
+       warning("comparison with string literal");
+
       /* If this is a comparison of a field, we may be able to simplify it.  */
       if (((TREE_CODE (arg0) == COMPONENT_REF
            && (*lang_hooks.can_use_bit_fields_p) ())





------- You are receiving this mail because: -------
You are on the CC list for the bug, or are watching someone who is.



More information about the Gcc-bugs mailing list