This is the mail archive of the gcc@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: -Wcast-qual and casting away


Ian Lance Taylor wrote:
> Consider this C/C++ program:
> 
> extern void **f1();
> void f2(const char *p) { *(const void **)f1() = p; }
> 
> If I compile this program with g++ -Wcast-qual, I get this:
> 
> foo2.cc:2: warning: cast from type âvoid**â to type âconst void**â casts away qualifiers
> 
> If I compile this program with gcc -Wcast-qual, I do not get any
> warning.

> 2) Change the C frontend to also warn about this case, albeit with a
>    better message.

> Of course in all cases the frontends should continue to warn about a
> cast from const void** to void**.

  Well, they should warn about this case also then.

extern void **f1();
void f2(const char *p) { *(const void **)f1() = p; }

void *x;

void **f1()
{
  return &x
}

void f2(char *foo, int n)
{
  memcpy (x, foo, n); // BOOM!
}

  This looks like a disguised way of casting a const char* to a (non-const)
void* to me, isn't it?  Sure, the warning is being tripped by the cast rather
than the conversion that the cast is hiding, it needs to be improved, but I'm
glad we got at least some noise from such a dubious construction.

    cheers,
      DaveK


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