Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 282083 - slave.cpp:475: warning: cast from type 'char**' to type 'const char**' casts away constness
Summary: slave.cpp:475: warning: cast from type 'char**' to type 'const char**' casts ...
Status: RESOLVED INVALID
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: [OLD] Core system (show other bugs)
Hardware: All Linux
: High normal (vote)
Assignee: Gentoo Linux bug wranglers
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-08-20 10:20 UTC by David van Laatum
Modified: 2009-08-21 08:24 UTC (History)
1 user (show)

See Also:
Package list:
Runtime testing required: ---


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description David van Laatum 2009-08-20 10:20:13 UTC
g++ -g -MD -MP -Wall -O -Wuninitialized -Wunused -pedantic -Wformat -Winit-self -Wreturn-type -Wswitch-enum -Wunused -Wuninitialized -Wfloat-equal -Wshadow -Wpointer-arith -Wcast-qual -Wsign-compare -Wredundant-decls -Wno-long-long -I /usr/local/include/ -DHAVE_SENDFILE -fno-strict-aliasing -pipe -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/include/gdbm -I/usr/lib/perl5/5.8.8/i486-linux/CORE -DHAS_SETRESUID_PROTO -DHAS_SETRESGID_PROTO   -c -o slave.o slave.cpp
slave.cpp: In member function 'bool CSlave::AddQueueIDToLog(SMTPData*)':
slave.cpp:475: warning: cast from type 'char**' to type 'const char**' casts away constness

I would call this valid it the other way round thats wrong...

gcc --version
gcc (Gentoo 4.3.2-r3 p1.6, pie-10.1.5) 4.3.2

This is while compiling my own programs

Reproducible: Always
Comment 1 Samuli Suominen (RETIRED) gentoo-dev 2009-08-20 10:35:55 UTC
-Wcast-qual

Warn whenever a pointer is cast so as to remove a type qualifier
from the target type.  For example, warn if a "const char *" is
cast to an ordinary "char *".
Comment 2 David van Laatum 2009-08-20 10:43:48 UTC
(In reply to comment #1)
> -Wcast-qual
> 
> Warn whenever a pointer is cast so as to remove a type qualifier
> from the target type.  For example, warn if a "const char *" is
> cast to an ordinary "char *".
> 

thats my point casting so as to remove the const is wrong but adding it isn't.... which is what its complaining about in this case
Comment 3 David van Laatum 2009-08-20 10:50:04 UTC
#include <stdlib.h>

int main ( int argc, char **argv ) {
        char *buffer[1] = { NULL };
        const char **test = (const char**)buffer;
        return 0;
}
gcc -o test -Wcast-qual test.cpp 
test.cpp: In function 'int main(int, char**)':
test.cpp:5: warning: cast from type 'char**' to type 'const char**' casts away constness
Comment 4 David van Laatum 2009-08-20 10:57:57 UTC
no warning if compiled with gcc 3.4.6-r2
Comment 5 Jonathan Callen (RETIRED) gentoo-dev 2009-08-21 08:24:27 UTC
The warning is correct.  A cast from 'char**' to 'const char**' is not as harmless as you might at first think.  If it were legal to convert without a cast, it would enable the following:

const char a = 'a'; // this is a constant it should never change
char *x;
const char **y = &x; // in reality this is illegal
*y = &a; // now x points to a
*x = 'b'; // now we have modified a

As this would allow modifying 'const' variables, it is disallowed.  You can, however, convert 'char**' to 'const char* const*', which doesn't have the same issues.