New submission from Oren Milman:

I am not sure whether such a platform exists, but on a platform where
SIZEOF_VOID_P < SIZEOF_LONG, PyLong_AsVoidPtr (which is in
Objects/longobject.c) is:
    long x;
    if (PyLong_Check(vv) && _PyLong_Sign(vv) < 0)
        x = PyLong_AsLong(vv);
        x = PyLong_AsUnsignedLong(vv);
    if (x == -1 && PyErr_Occurred())
        return NULL;
    return (void *)x;
Thus, for example, 'PyLong_AsVoidPtr(PyLong_FromUnsignedLong(ULONG_MAX))'
would silently truncate ULONG_MAX, and return without an error.

An easy fix would be (mainly) to add to PyLong_AsVoidPtr
'Py_BUILD_ASSERT(SIZEOF_LONG <= SIZEOF_VOID_P);', but I am not sure we can
make that assumption.

Note that a compile time error is already raised:
    - by Objects/longobject.h, in case SIZEOF_VOID_P is different from
    - by Modules/_multiprocessing/multiprocessing.h, in case SIZEOF_VOID_P is
      different from SIZEOF_LONG and SIZEOF_LONG_LONG

components: Interpreter Core
messages: 288984
nosy: Oren Milman
priority: normal
severity: normal
status: open
title: potential silent truncation in PyLong_AsVoidPtr
type: behavior
versions: Python 3.7

Python tracker <>
Python-bugs-list mailing list

Reply via email to