[PATCH:libX11 1/2] Bug 32098: Making XInternAtoms take const char ** breaks callers

Aaron Plattner aplattner at nvidia.com
Wed Dec 15 11:15:52 PST 2010


On Tue, Dec 14, 2010 at 03:48:50PM -0800, Simon Thum wrote:
> On 12/14/2010 10:01 PM, Alan Coopersmith wrote:
> > Unfortunately, this was a surprise to me, since I was expecting it to
> > behave as const char * does when passed char * pointers, as apparently
> > did everyone on xorg-devel when the patch went by.
> 
> Another myth debunked: "applying const gets you benefits".
> 
> Thank you, at least I feel less stupid now!
> 
> But wouldn't all that mean the correct decl would be "const char* const
> *"? Then no-one would have to assume a temporary might get modified. (I
> didn't read into it too deep, however)

Right.  If you have implicit conversions from char** to const char** then
you can do this:

int main() {
    const char cc = 'a';
    char *p;
    char **pp = &p;

    // implicit conversion from char** to const char**
    const char **cpp = pp;

    // assignment of a const char* to the pointer pointed to by cpp (i.e. p);
    *cpp = &cc;
    *p = 'x';

    // cc is now 'x' despite being const and initialized to 'a'.
    printf("%c\n", cc);
    return 0;
}

$ gcc test.c
test.c: In function ‘main’:
test.c:11: warning: initialization from incompatible pointer type
$ ./a.out
x
$ gcc -O2 test.c
test.c: In function ‘main’:
test.c:11: warning: initialization from incompatible pointer type
$ ./a.out
a


More information about the xorg-devel mailing list