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: [PATCH INSTALLED]: Fix -Wc++-compat warnings in toplevel [t-z]*.c files


Gabriel Dos Reis wrote:
> On Mon, Jun 23, 2008 at 10:49 AM, Andrew Haley <aph@redhat.com> wrote:
>> Kaveh R. Ghazi wrote:
>>> From: "Andrew Haley" <aph@redhat.com>
>>>
>>>> Having to cast the return value of malloc-like
>>>> functions is the worst offender, and perhaps we should write some
>>>> rules that forbid it.  This would require best practice to be
>>>> discussed and established, along with some support code.
>>>> Andrew.
>>> I don't see casting the return value of malloc-like functions is so
>>> terrible.
>> Yeah it is, because it suppresses a useful warning or error message if
>> the function isn't defined as returning an appropriate type.  The cast
>> will swallow the warning and you may miss the error.
> 
> The real problem is that of a missing declaration.

No, that's not right.  The function may well be declared as returning
a different type, and by the cast you lose all type checking.

> GCC already has ways to detect that -- and we use it.
> So, no useful warning or error message is lost.

I'm afraid it is.  Consider this one:

extern long alloc_poo(void);

long z()
{
  long *d = (long *)alloc_poo();
  return *d;
}

Or this one:

extern int *alloc_poo(void);

long z()
{
  long *d = (long *)alloc_poo();
  return *d;
}

Even with -Wall we get no warnings.
Is this error unlikely?  I don't think so.

Andrew.


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