[New-bugs-announce] [issue29720] potential silent truncation in PyLong_AsVoidPtr

Oren Milman report at bugs.python.org
Sat Mar 4 11:12:04 EST 2017


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);
    else
        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
      SIZEOF_INT, SIZEOF_LONG and SIZEOF_LONG_LONG
    - 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 <report at bugs.python.org>
<http://bugs.python.org/issue29720>
_______________________________________


More information about the New-bugs-announce mailing list