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

Philipp Thomas pth at suse.de
Mon Dec 5 14:17:55 CET 2005

* David Abrahams (dave at boost-consulting.com) [20051201 20:06]:

> >     return (detail::new_reference)PyObject_CallFunction(
> >         (PyObject*)&PyString_Type, "(O)",
> >         arg_.ptr());
> >

> I'm pretty sure this warning is bogus

It's not. AFAICS, PyObject and PyString_Type aren't related in any way. So
casting &PyString_Type to 'PyObject *' breaks aliasing rules and
dereferencing the type-punned pointer does break strict-aliasing analysis.

> The patch may be bogus, too, at least as far as standard C++ is
> concerned.  C++ doesn't support the C99 restrict feature.

It doesn't have to anything with restrict! It's the simple rule that an
object (non-C++ meaning) may only be accessed by either a pointer of the
same type or a char/void pointer.

>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.

The aliasing issue is the same in C89, it just isn't spelled out as 
precisely as C99 does. That's also why gcc since 3.0 gives this warning for
all C code, no matter if it's C89 or C99.

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

gcc bases parts of its optimisations on the result of the strict-alias
analysis. So for instance objects (still non-C++ sense) will be eliminated
if gcc determines that they aren't accessed by a pointer of the allowed
type. So an access via a type-punned pointer will return garbage in this


More information about the Cplusplus-sig mailing list