public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* middle-end/3773: Comparision with string literal
@ 2001-07-23  3:56 ank
  0 siblings, 0 replies; 2+ messages in thread
From: ank @ 2001-07-23  3:56 UTC (permalink / raw)
  To: gcc-gnats

>Number:         3773
>Category:       middle-end
>Synopsis:       Comparision with string literal
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Mon Jul 23 03:56:01 PDT 2001
>Closed-Date:
>Last-Modified:
>Originator:     ank@nixu.fi (Alexander Krotov)
>Release:        3.0
>Organization:
>Environment:
All
>Description:
GCC 3.0 does not warn about typical beginers misstake:
int f (char *p)
{
	if (p=="foo")
		return 1;
	return 2;
}
And does not actually recognize that condition is always false.
>How-To-Repeat:
Compile code in descrption.
>Fix:
Might be fixed in fold-const.c:
6153a6154,6164
>       if (TREE_CODE(a0)==ADDR_EXPR && TREE_CODE(TREE_OPERAND(a0,0))==STRING_CST 
>        || TREE_CODE(a1)==ADDR_EXPR && TREE_CODE(TREE_OPERAND(a1,0))==STRING_CST)
>       {
>         warning ("comparison is always %d due to comparision with string "
>                  "literal",
>                  code != EQ_EXPR);
>         return convert (type,
>                         (code != EQ_EXPR
>                          ? integer_one_node : integer_zero_node));
>       }
> 
Although this is not the best way to go, it would be better
to detect the case after constant propagation (in cse.c)
to recognize cases like
int f (char *p)
{
	char *x = "foo";
	if (p==x)
		return 1;
	return 2;
}
>Release-Note:
>Audit-Trail:
>Unformatted:


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

* Re: middle-end/3773: Comparision with string literal
@ 2002-04-04  4:21 rth
  0 siblings, 0 replies; 2+ messages in thread
From: rth @ 2002-04-04  4:21 UTC (permalink / raw)
  To: ank, gcc-bugs, gcc-prs, nobody

Synopsis: Comparision with string literal

State-Changed-From-To: open->feedback
State-Changed-By: rth
State-Changed-When: Thu Apr  4 04:21:29 2002
State-Changed-Why:
    I don't think it's that simple.  Consider
    
    char *f(char *p)
    {
      if (p == "foo")
        return 0;
      else
        return "foo";
    }
    
    bool g()
    {
      f(f(0)) == 0;
    }
    
    While one could make the claim that one shouldn't expect G to
    always return true, it will in practice, because virtually all
    compilers instantiate each string only once.  (GCC's exception
    being -fwritable-strings.)
    
    One *could* simply warn for comparisons against string literals
    without inferring anything about the result.  If someone wants
    to submit a patch for that, I guess that would be useful.  Don't
    forget documentation and testsuite updates for it if so...

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=3773


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

end of thread, other threads:[~2002-04-04 12:21 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-07-23  3:56 middle-end/3773: Comparision with string literal ank
2002-04-04  4:21 rth

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