https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91336

            Bug ID: 91336
           Summary: Missing -Wcast-qual?
           Product: gcc
           Version: 9.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hahnjo at hahnjo dot de
  Target Milestone: ---

Created attachment 46665
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=46665&action=edit
Test case.

If I compile the attached example with
> $ g++ -c -Wall -Wextra -Wcast-qual cast-qual.cpp
I get many (correct) errors, but only one warning enabled by -Wcast-qual.

I think GCC is correct to flag the following:
> static const char cvar[4] = { 0 };
> [...]
> const char **p2 = (const char **)&cvar;
> *p2 = "bla";
output:
> cast-qual.cpp:20:36: warning: cast from type ‘const char (*)[4]’ to type 
> ‘const char**’ casts away qualifiers [-Wcast-qual]
In this case, cvar is an array and is only cast to a pointer, so you should not
assign to *p2. Instead the correct cast is
> const char * const *p3 = (const char * const *)&cvar;
and the assignment *p3 = "bla" leads to
> cast-qual.cpp:27:6: error: assignment of read-only location ‘* p3’

If the code instead defines
> static char var[4] = { 0 };
then the cast
> char **p2 = (char **)&var;
does not lead to a warning while assigning to *p2 should still not work.
Similarly to above, I think the correct cast should be
> char * const *p3 = (char * const *)&var;

Do you agree?

Reply via email to