This is the mail archive of the gcc-patches@gcc.gnu.org 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]
Other format: [Raw text]

Re: [committed] fix spurious strstr(s, "") warning


> Richard Guenther wrote:
>> On 11/19/05, James E Wilson <wilson@specifix.com> wrote:
>> The optimizer is converting
>>   cp = strstr (s, "");
>> to just
>>   cp = s;
>> Unfortunately, the argument s to strstr is const char *, and the result
>> of strstr is char *, so we have a type mismatch here after
>> optimization.  This generates a spurious warning when compiled with
>> -Wall.
>> ...
>> The solution here is easy.  We just need to convert the argument to the
>> result type before returning it, so that effectively we are generating
>>  cp = (char *) s;

It would seem that it's not a solution; it only masks the bug introduced by
an incorrect optimization.

> Of course this kind of prototype is really stupid.  Either it should return
> const char*, or take char*.  But well, const-correctness in C is hard
> anyway, and it's not us to decide...

Actually the the warning seems fully correct, as the optimization is truly
wrong (and dangerous), as strstr's const char * arguments warrant they won't
be modified (even indirectly if effectively aliased as a result of the
function's result assignment to a type which is modifiable).

As arguably the optimization is only valid if the result of strstr is
correspondingly assigned to a const char * itself, or a variable known
never potentially modified, it would seem?




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