This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: C: Proposal: Warning using char* a; if(a == "")

I wrote:
> > So, I do think that the warning makes sense.  A logical place to put
> > the warning would be in the truthvalue_conversion function.  Given
> > a relational expression, one of {EQ,NE,LE,GE,LT,GT}_EXPR, if one side
> > is a string literal (note: an actual literal, not a symbol whose value
> > is a literal), this is almost certainly invalid code, as equality
> > comparison will always be false (unless there is a pass that combines
> > duplicate string literals, something the programmer cannot count on).
> Wrong on some platform he can count on this. Not every developer
> out there develops for crossplatform.

I do not believe that you can name me a platform where code of this form
is safe. Even if the compiler collapses matching strings in the same
compilation unit, it is almost certain that the same string could exist
with a different address elsewhere in the tool.  The detailed comments
a competent programmer would have to write to explain the assumptions made
(about why this is safe) would be more work than just doing the coding
right (replacing the literal string with a static const char array or

> > Suggested text:
> > warning: direct comparison to string literal (did you mean to use `strcmp'?)
> Please add the following line maybe?:
> 	Proceed to compile? [yes/no] (please type literally):

Why do you want that?  We're talking about adding a WARNING.  Of course
we proceed to compile the code!

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]