Author petr.viktorin
Recipients cstratak, mark.dickinson, meador.inge, petr.viktorin, vstinner
Date 2020-02-27.16:32:48
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1582821168.91.0.778257387599.issue39689@roundup.psfhosted.org>
In-reply-to
Content
C compiler dev that it's indeed undefined behavior.

> Quick and obvious fix:
> 
>       static PyObject *
>       nu_bool(const char *p, const formatdef *f)
>       {
>           char x;
>           memcpy((char *)&x, p, sizeof x);
>           return PyBool_FromLong(x != 0);
>       }
>
> Which is optimized to
>
>     static PyObject *
>     nu_bool(const char *p, const formatdef *f)
>     {
>         return PyBool_FromLong(*p != 0);
>     }


I'm left with a question for CPython's struct experts:

The above would be my preferred fix, but the Python code is asking to convert a memory buffer to bool *using platform-specific semantics*.
Is this fix OK if C treats a \xf0 _Bool as falsey?


(Also, this assumes size of _Bool is the same as size of char.
I guess we can add a build-time assertion for that, and say we don't support platforms where that's not the case.)
History
Date User Action Args
2020-02-27 16:32:48petr.viktorinsetrecipients: + petr.viktorin, mark.dickinson, vstinner, meador.inge, cstratak
2020-02-27 16:32:48petr.viktorinsetmessageid: <1582821168.91.0.778257387599.issue39689@roundup.psfhosted.org>
2020-02-27 16:32:48petr.viktorinlinkissue39689 messages
2020-02-27 16:32:48petr.viktorincreate