public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Gabriel Dos Reis <dosreis@gmail.com>
To: Ian Lance Taylor <iant@google.com>
Cc: Richard Guenther <richard.guenther@gmail.com>,
	Andreas Schwab <schwab@linux-m68k.org>,
	gcc@gcc.gnu.org
Subject: Re: -Wcast-qual and casting away
Date: Fri, 22 May 2009 01:01:00 -0000	[thread overview]
Message-ID: <206fcf960905211036l126549bfga7ecac3ad4bc3d9f@mail.gmail.com> (raw)
In-Reply-To: <m34ovewi4l.fsf@google.com>

On Thu, May 21, 2009 at 11:11 AM, Ian Lance Taylor <iant@google.com> wrote:
> Richard Guenther <richard.guenther@gmail.com> writes:
>
>> On Thu, May 21, 2009 at 1:50 PM, Andreas Schwab <schwab@linux-m68k.org> wrote:
>>> Ian Lance Taylor <iant@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	other threads:[~2009-05-21 17:36 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-05-21 11:50 Ian Lance Taylor
2009-05-21 11:58 ` Richard Guenther
2009-05-22  0:20   ` Gabriel Dos Reis
2009-05-21 12:09 ` Andreas Schwab
2009-05-21 12:50   ` Richard Guenther
2009-05-21 15:13     ` Andreas Schwab
2009-05-21 15:22     ` Sebastian Redl
2009-05-21 22:48     ` Ian Lance Taylor
2009-05-22  1:01       ` Gabriel Dos Reis [this message]
2009-05-21 12:38 ` Joseph S. Myers
2009-05-21 17:36   ` Ian Lance Taylor
2009-05-21 15:26 ` Dave Korn
2009-05-22  0:16 ` Gabriel Dos Reis

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=206fcf960905211036l126549bfga7ecac3ad4bc3d9f@mail.gmail.com \
    --to=dosreis@gmail.com \
    --cc=gcc@gcc.gnu.org \
    --cc=gdr@integrable-solutions.net \
    --cc=iant@google.com \
    --cc=richard.guenther@gmail.com \
    --cc=schwab@linux-m68k.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).