On Mon, 7 Feb 2022 15:29:39 +0100 Victor Stinner <vstin...@python.org> wrote: > On Mon, Feb 7, 2022 at 2:26 PM Victor Stinner <vstin...@python.org> wrote: > > CPython is also affected by these issues, but the benefits of PEP 674 > > (alone) are too indirect, so I chose to avoid mentioning CPython > > issues directly, to avoid confusion. > > A concrete example of problem caused by exposing structures in the C > API (unrelated to PEP 674). It's a tricky problem... > > typedef struct { > PyObject_VAR_HEAD > Py_hash_t ob_shash; > char ob_sval[1]; > } PyBytesObject; > > The "char ob_sval[1];" syntax used to declare an array is an undefined > behavior if the array is longer in memory. On a bytes object of 4 > bytes, accessing ob_sval[3] works, but is an undefined behavior. > > => see https://bugs.python.org/issue40120 for details > > The problem can be solved by using "char ob_sval[];" syntax, but we > cannot use this syntax in the public C header, since it causes > compiler errors if the header is built with a C++ compiler (not to > build Python itself, but build a C++ extension using the Python C > API). Removing the structure from the public C API would solve the C++ > issue.
You could also have something like: typedef struct { PyObject_VAR_HEAD Py_hash_t ob_shash; #ifdef __cpluscplus char ob_sval[1]; #else char ob_sval[]; #endif } PyBytesObject; Regards Antoine. _______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-le...@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/HWXZIISALVHAHT4HSBG33S7IFKLIYBNG/ Code of Conduct: http://python.org/psf/codeofconduct/