Skip to content

[3.7] bpo-37170: Fix the cast on error in PyLong_AsUnsignedLongLongMask() (GH-13860) #13896

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Doc/c-api/long.rst
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@ distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.
If the value of *obj* is out of range for an :c:type:`unsigned long`,
return the reduction of that value modulo ``ULONG_MAX + 1``.

Returns ``-1`` on error. Use :c:func:`PyErr_Occurred` to disambiguate.
Returns ``(unsigned long)-1`` on error. Use :c:func:`PyErr_Occurred` to
disambiguate.


.. c:function:: unsigned long long PyLong_AsUnsignedLongLongMask(PyObject *obj)
Expand All @@ -271,7 +272,8 @@ distinguished from a number. Use :c:func:`PyErr_Occurred` to disambiguate.
If the value of *obj* is out of range for an :c:type:`unsigned long long`,
return the reduction of that value modulo ``PY_ULLONG_MAX + 1``.

Returns ``-1`` on error. Use :c:func:`PyErr_Occurred` to disambiguate.
Returns ``(unsigned long long)-1`` on error. Use :c:func:`PyErr_Occurred`
to disambiguate.


.. c:function:: double PyLong_AsDouble(PyObject *pylong)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix the cast on error in :c:func:`PyLong_AsUnsignedLongLongMask()`.
22 changes: 22 additions & 0 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,26 @@ test_long_as_size_t(PyObject *self)
return Py_None;
}

static PyObject *
test_long_as_unsigned_long_long_mask(PyObject *self,
PyObject *Py_UNUSED(ignored))
{
unsigned long long res = PyLong_AsUnsignedLongLongMask(NULL);

if (res != (unsigned long long)-1 || !PyErr_Occurred()) {
return raiseTestError("test_long_as_unsigned_long_long_mask",
"PyLong_AsUnsignedLongLongMask(NULL) didn't "
"complain");
}
if (!PyErr_ExceptionMatches(PyExc_SystemError)) {
return raiseTestError("test_long_as_unsigned_long_long_mask",
"PyLong_AsUnsignedLongLongMask(NULL) raised "
"something other than SystemError");
}
PyErr_Clear();
Py_RETURN_NONE;
}

/* Test the PyLong_AsDouble API. At present this just tests that
non-integer arguments are handled correctly.
*/
Expand Down Expand Up @@ -4663,6 +4683,8 @@ static PyMethodDef TestMethods[] = {
METH_NOARGS},
{"test_long_as_double", (PyCFunction)test_long_as_double,METH_NOARGS},
{"test_long_as_size_t", (PyCFunction)test_long_as_size_t,METH_NOARGS},
{"test_long_as_unsigned_long_long_mask",
(PyCFunction)test_long_as_unsigned_long_long_mask, METH_NOARGS},
{"test_long_numbits", (PyCFunction)test_long_numbits, METH_NOARGS},
{"test_k_code", (PyCFunction)test_k_code, METH_NOARGS},
{"test_empty_argparse", (PyCFunction)test_empty_argparse,METH_NOARGS},
Expand Down
4 changes: 2 additions & 2 deletions Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1309,7 +1309,7 @@ _PyLong_AsUnsignedLongLongMask(PyObject *vv)

if (vv == NULL || !PyLong_Check(vv)) {
PyErr_BadInternalCall();
return (unsigned long) -1;
return (unsigned long long) -1;
}
v = (PyLongObject *)vv;
switch(Py_SIZE(v)) {
Expand Down Expand Up @@ -1337,7 +1337,7 @@ PyLong_AsUnsignedLongLongMask(PyObject *op)

if (op == NULL) {
PyErr_BadInternalCall();
return (unsigned long)-1;
return (unsigned long long)-1;
}

if (PyLong_Check(op)) {
Expand Down