This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: -Wcast-qual and casting away
- From: Dave Korn <dave dot korn dot cygwin at googlemail dot com>
- To: Ian Lance Taylor <iant at google dot com>
- Cc: gcc at gcc dot gnu dot org
- Date: Thu, 21 May 2009 13:49:30 +0100
- Subject: Re: -Wcast-qual and casting away
- References: <m3r5yjxcpw.fsf@google.com>
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