This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author vstinner
Recipients gregory.p.smith, pablogsal, vstinner
Date 2022-03-03.16:20:36
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1646324436.55.0.146677749703.issue46913@roundup.psfhosted.org>
In-reply-to
Content
test_ctypes: test_shorts() of ctypes.test.test_bitfields.C_Test is failing with:

---
test_shorts (ctypes.test.test_bitfields.C_Test) ... /home/vstinner/python/main/Modules/_ctypes/cfield.c:554:5: runtime error: shift exponent 18446744073709551614 is too large for 16-bit type 'short'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /home/vstinner/python/main/Modules/_ctypes/cfield.c:554:5 in 
---

It's a test on the "h" format code:
----
#define LOW_BIT(x)  ((x) & 0xFFFF)
#define NUM_BITS(x) ((x) >> 16)

#define GET_BITFIELD(v, size)                                           \
    if (NUM_BITS(size)) {                                               \
        v <<= (sizeof(v)*8 - LOW_BIT(size) - NUM_BITS(size));           \
        v >>= (sizeof(v)*8 - NUM_BITS(size));                           \

static PyObject *
h_get(void *ptr, Py_ssize_t size)
{
    short val;
    memcpy(&val, ptr, sizeof(val));
    GET_BITFIELD(val, size); // <==== HERE
    return PyLong_FromLong((long)val);
}

static struct fielddesc formattable[] = {
    ...
    { 'h', h_set, h_get, NULL, h_set_sw, h_get_sw},
    ...
};
----
History
Date User Action Args
2022-03-03 16:20:36vstinnersetrecipients: + vstinner, gregory.p.smith, pablogsal
2022-03-03 16:20:36vstinnersetmessageid: <1646324436.55.0.146677749703.issue46913@roundup.psfhosted.org>
2022-03-03 16:20:36vstinnerlinkissue46913 messages
2022-03-03 16:20:36vstinnercreate