On Thu, May 21, 2009 at 11:11 AM, Ian Lance Taylor <i...@google.com> wrote:
> Richard Guenther <richard.guent...@gmail.com> writes:
>
>> On Thu, May 21, 2009 at 1:50 PM, Andreas Schwab <sch...@linux-m68k.org> 
>> wrote:
>>> Ian Lance Taylor <i...@google.com> writes:
>>>
>>>> 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
>>>
>>> In a sense this warning is actually correct: this is storing a const
>>> char * into a void * object, which is where the qualifier is lost.  IMHO
>>> having a warning for this questionable operation is a good thing.
>>
>> I don't think so.
>>
>> extern char **f1();
>> void f(char *p)
>> {
>>   *(const char **)f1() = p;
>> }
>>
>> warns the same. typeof(*(const char **)) should still be const char *.
>>
>> For
>>
>> extern const char **f1();
>> void f(char *p)
>> {
>>   *(char **)f1() = p;
>> }
>>
>> it warns with
>>
>> t.C: In function ‘void f(char*)’:
>> t.C:4: warning: cast from type ‘const char**’ to type ‘char**’ casts
>> away constness
>>
>> which makes sense.
>
> Let's not focus too much on the operation (the indirection and the
> assignment).  The warning is about the cast itself.  Should we issue
> that warning or not?  Others have explained the cases where the cast can
> lead to unsafe code.

The particular cast in question is not a safe operation.  Should we warn
about it when -Wcast-qual, I think so -- that is one of the purposes of
the switch.

-- Gaby

Reply via email to