[C++-sig] boost/python 1.33.1 breaks aliasing rules

David Abrahams dave at boost-consulting.com
Thu Dec 1 19:55:33 CET 2005

"Ralf W. Grosse-Kunstleve" <rwgk at yahoo.com> writes:

> --- Philipp Thomas <pth at suse.de> wrote:
>> We use a patch for gcc 4.1 that also makes it warn about violations of the
>> C/C++ aliasing
>> rules in C++ code (stock gcc only warns for C code). For the code in boost
>> 1.33.1 I get
>> warning: dereferencing type-punned pointer will break strict-aliasing rules
>> for the following files:
>>  /usr/src/packages/BUILD/boost_1_33_1/libs/python/build/../src/long.cpp:
>> 12,19, 26
>>  /usr/src/packages/BUILD/boost_1_33_1/libs/python/build/../src/dict.cpp: 32
>>  /usr/src/packages/BUILD/boost_1_33_1/libs/python/build/../src/tuple.cpp:12
>>  /usr/src/packages/BUILD/boost_1_33_1/libs/python/build/../src/str.cpp: 12
> Thanks for pointing out the aliasing problems! Could you help us understanding
> how the problem could be fixed? E.g., this is the code in str.cpp:
>     return (detail::new_reference)PyObject_CallFunction(
>         (PyObject*)&PyString_Type, "(O)",
>         arg_.ptr());
> Line 12 is (PyObject*)&PyString_Type. Is this what we have to look at?
> Both types are defined in Python headers. AFAIK the cast above is standard
> practice in Python code...

I'm pretty sure this warning is bogus, since there are no two pointers
being passed here that the compiler can deduce point into the same object.


The patch may be bogus, too, at least as far as standard C++ is
concerned.  C++ doesn't support the C99 restrict feature.  C++ is
built on C89 or C90 (I forget which).  Does Python even support being
compiled with C99?  If not, this whole thing is a red herring.

Can Philipp explain why we should invest energy in trying to make
these warnings go away?

Dave Abrahams
Boost Consulting

More information about the Cplusplus-sig mailing list