public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/7776] const char* p = "foo"; if (p == "foo") ... is compiled without warning!
       [not found] <20020830090600.7776.udbz@rz.uni-karlsruhe.de>
@ 2003-05-24 14:40 ` pinskia@physics.uc.edu
  2003-05-24 18:53 ` udbz@rz.uni-karlsruhe.de
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 9+ messages in thread
From: pinskia@physics.uc.edu @ 2003-05-24 14:40 UTC (permalink / raw)
  To: gcc-bugs

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


pinskia@physics.uc.edu changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever Confirmed|                            |1
   Last reconfirmed|0000-00-00 00:00:00         |2003-05-24 14:08:34
               date|                            |


------- Additional Comments From pinskia@physics.uc.edu  2003-05-24 14:08 -------
confirmed on mainline (20030524).
Should there be a warning on comparing a char* to string constant, suggesting strcmp?



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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c/7776] const char* p = "foo"; if (p == "foo") ... is compiled without warning!
       [not found] <20020830090600.7776.udbz@rz.uni-karlsruhe.de>
  2003-05-24 14:40 ` [Bug c/7776] const char* p = "foo"; if (p == "foo") ... is compiled without warning! pinskia@physics.uc.edu
@ 2003-05-24 18:53 ` udbz@rz.uni-karlsruhe.de
  2003-05-25  1:35 ` falk.hueffner@student.uni-tuebingen.de
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 9+ messages in thread
From: udbz@rz.uni-karlsruhe.de @ 2003-05-24 18:53 UTC (permalink / raw)
  To: gcc-bugs

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 udbz@rz.uni-karlsruhe.de  2003-05-24 18:43 -------
Subject: Re: [Bug c/7776] const char* p = "foo"; if (p == "foo") ... is
 compiled without warning!

Hi,

On 24 May 2003, pinskia@physics.uc.edu wrote:

> ------- Additional Comments From pinskia@physics.uc.edu  2003-05-24 14:08 -------
> confirmed on mainline (20030524).
> Should there be a warning on comparing a char* to string constant,
> suggesting strcmp?

Yes, that was the idea.

Greetings,
Peter

---
Peter Schlaile  ***  eMail udbz@rz.uni-karlsruhe.de

So Linus, what are we doing tonight?
          The same thing we do every night Tux. Try to take over the world!





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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c/7776] const char* p = "foo"; if (p == "foo") ... is compiled without warning!
       [not found] <20020830090600.7776.udbz@rz.uni-karlsruhe.de>
  2003-05-24 14:40 ` [Bug c/7776] const char* p = "foo"; if (p == "foo") ... is compiled without warning! pinskia@physics.uc.edu
  2003-05-24 18:53 ` udbz@rz.uni-karlsruhe.de
@ 2003-05-25  1:35 ` falk.hueffner@student.uni-tuebingen.de
  2003-05-25  1:49 ` jsm28@cam.ac.uk
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 9+ messages in thread
From: falk.hueffner@student.uni-tuebingen.de @ 2003-05-25  1:35 UTC (permalink / raw)
  To: gcc-bugs

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.


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug c/7776] const char* p = "foo"; if (p == "foo") ... is compiled without warning!
       [not found] <20020830090600.7776.udbz@rz.uni-karlsruhe.de>
                   ` (2 preceding siblings ...)
  2003-05-25  1:35 ` falk.hueffner@student.uni-tuebingen.de
@ 2003-05-25  1:49 ` jsm28@cam.ac.uk
  2005-06-06 15:06 ` [Bug middle-end/7776] " roger at eyesopen dot com
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 9+ messages in thread
From: jsm28@cam.ac.uk @ 2003-05-25  1:49 UTC (permalink / raw)
  To: gcc-bugs

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 jsm28@cam.ac.uk  2003-05-25 01:29 -------
Subject: Re: [Bug c/7776] const char* p = "foo"; if (p == "foo") ... is
 compiled without warning!

On Sun, 25 May 2003, Falk Hueffner wrote:

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

E.g., an optimised string function macro that first compares the pointers
(which might be to string constants) before looking at contents only if
necessary.  glibc headers have contained funny expressions that expect 
multiple copies of a string constant argument to be the same, and 
differences such as &"foo"[1] - "foo" to be folded, if not actually 
evaluating "foo" == "foo".  If you know that the implementation merges 
identical string literals, this is a reasonable thing to do.

I don't believe this should be a mandatory warning - in general those (as 
opposed to mandatory pedwarns) should be avoided; a warning within -Wall 
would be the natural place, except that the possibility of this arising in 
conjunction with macros may mean -W is more appropriate.





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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug middle-end/7776] const char* p = "foo"; if (p == "foo") ... is compiled without warning!
       [not found] <20020830090600.7776.udbz@rz.uni-karlsruhe.de>
                   ` (3 preceding siblings ...)
  2003-05-25  1:49 ` jsm28@cam.ac.uk
@ 2005-06-06 15:06 ` roger at eyesopen dot com
  2005-06-06 15:49 ` trt at acm dot org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 9+ messages in thread
From: roger at eyesopen dot com @ 2005-06-06 15:06 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From roger at eyesopen dot com  2005-06-06 15:06 -------
C front-end patch here: http://gcc.gnu.org/ml/gcc-patches/2005-06/msg00369.html


-- 
           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch


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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug middle-end/7776] const char* p = "foo"; if (p == "foo") ... is compiled without warning!
       [not found] <20020830090600.7776.udbz@rz.uni-karlsruhe.de>
                   ` (4 preceding siblings ...)
  2005-06-06 15:06 ` [Bug middle-end/7776] " roger at eyesopen dot com
@ 2005-06-06 15:49 ` trt at acm dot org
  2005-06-19  1:49 ` roger at eyesopen dot com
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 9+ messages in thread
From: trt at acm dot org @ 2005-06-06 15:49 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From trt at acm dot org  2005-06-06 15:48 -------
I recommend a version with fewer false positives.
I've been using a warning like this for years, with zero false positives.
The current gcc-4-ified version is:

-   /* check for comparing string constant with anything besides simple zero */
-   if (TREE_CODE_CLASS (code) == tcc_comparison && extra_warnings
-       && (code1 == STRING_CST) != (code2 == STRING_CST)
-       && !integer_zerop (arg1.value) && !integer_zerop (arg2.value))
-     warning (0, "comparison of pointer with string literal");

An older suggestion is http://gcc.gnu.org/ml/gcc-patches/1999-10n/msg00548.html

-- 


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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug middle-end/7776] const char* p = "foo"; if (p == "foo") ... is compiled without warning!
       [not found] <20020830090600.7776.udbz@rz.uni-karlsruhe.de>
                   ` (5 preceding siblings ...)
  2005-06-06 15:49 ` trt at acm dot org
@ 2005-06-19  1:49 ` roger at eyesopen dot com
  2005-06-24 13:45 ` pluto at agmk dot net
  2005-06-24 13:51 ` pluto at agmk dot net
  8 siblings, 0 replies; 9+ messages in thread
From: roger at eyesopen dot com @ 2005-06-19  1:49 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From roger at eyesopen dot com  2005-06-19 01:49 -------
A revised patch, allowing equality and inequality comparisons against NULL, yet
retaining warnings for things like 'if ("foo" > 0)' and 'if ("foo" == "bar")'
was posted here:  http://gcc.gnu.org/ml/gcc-patches/2005-06/msg01177.html


-- 


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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug middle-end/7776] const char* p = "foo"; if (p == "foo") ... is compiled without warning!
       [not found] <20020830090600.7776.udbz@rz.uni-karlsruhe.de>
                   ` (6 preceding siblings ...)
  2005-06-19  1:49 ` roger at eyesopen dot com
@ 2005-06-24 13:45 ` pluto at agmk dot net
  2005-06-24 13:51 ` pluto at agmk dot net
  8 siblings, 0 replies; 9+ messages in thread
From: pluto at agmk dot net @ 2005-06-24 13:45 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pluto at agmk dot net  2005-06-24 13:44 -------
(In reply to comment #9)  
> A revised patch, allowing equality and inequality comparisons against NULL,  
yet  
> retaining warnings for things like 'if ("foo" > 0)' and 'if ("foo" ==  
"bar")'  
> was posted here:  http://gcc.gnu.org/ml/gcc-patches/2005-06/msg01177.html  
>   
  
with this patch I get an ice at amd64:  
  
(..)  
-c ../../gcc/unwind-dw2.c -o libgcc/./unwind-dw2.o  
  
In file included from ../../gcc/unwind-dw2.c:256:  
../../gcc/config/i386/linux-unwind.h:  
In function 'x86_64_fallback_frame_state': 
../../gcc/config/i386/linux-unwind.h:55: warning: dereferencing type-punned 
                                   pointer will break strict-aliasing rules  
../../gcc/unwind.inc: In function '_Unwind_ForcedUnwind': 
../../gcc/unwind.inc:215: internal compiler error: in create_pre_exit, 
                                                   at mode-switching.c:350  
 

-- 


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


^ permalink raw reply	[flat|nested] 9+ messages in thread

* [Bug middle-end/7776] const char* p = "foo"; if (p == "foo") ... is compiled without warning!
       [not found] <20020830090600.7776.udbz@rz.uni-karlsruhe.de>
                   ` (7 preceding siblings ...)
  2005-06-24 13:45 ` pluto at agmk dot net
@ 2005-06-24 13:51 ` pluto at agmk dot net
  8 siblings, 0 replies; 9+ messages in thread
From: pluto at agmk dot net @ 2005-06-24 13:51 UTC (permalink / raw)
  To: gcc-bugs


------- Additional Comments From pluto at agmk dot net  2005-06-24 13:50 -------
(In reply to comment #10) 
> (In reply to comment #9)   
> > A revised patch, allowing equality and inequality comparisons against 
NULL,   
> yet   
> > retaining warnings for things like 'if ("foo" > 0)' and 'if ("foo" ==   
> "bar")'   
> > was posted here:  http://gcc.gnu.org/ml/gcc-patches/2005-06/msg01177.html   
> >    
>    
> with this patch I get an ice at amd64:   
>    
> (..)   
> -c ../../gcc/unwind-dw2.c -o libgcc/./unwind-dw2.o   
>    
> In file included from ../../gcc/unwind-dw2.c:256:   
> ../../gcc/config/i386/linux-unwind.h:   
> In function 'x86_64_fallback_frame_state':  
> ../../gcc/config/i386/linux-unwind.h:55: warning: dereferencing type-punned  
>                                    pointer will break strict-aliasing rules   
> ../../gcc/unwind.inc: In function '_Unwind_ForcedUnwind':  
> ../../gcc/unwind.inc:215: internal compiler error: in create_pre_exit,  
>                                                    at mode-switching.c:350   
>   
 
oops, sorry. i've posted above in wrong PR. 

-- 


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


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2005-06-24 13:51 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20020830090600.7776.udbz@rz.uni-karlsruhe.de>
2003-05-24 14:40 ` [Bug c/7776] const char* p = "foo"; if (p == "foo") ... is compiled without warning! pinskia@physics.uc.edu
2003-05-24 18:53 ` udbz@rz.uni-karlsruhe.de
2003-05-25  1:35 ` falk.hueffner@student.uni-tuebingen.de
2003-05-25  1:49 ` jsm28@cam.ac.uk
2005-06-06 15:06 ` [Bug middle-end/7776] " roger at eyesopen dot com
2005-06-06 15:49 ` trt at acm dot org
2005-06-19  1:49 ` roger at eyesopen dot com
2005-06-24 13:45 ` pluto at agmk dot net
2005-06-24 13:51 ` pluto at agmk dot net

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).