Skip to content

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

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: 6 additions & 0 deletions Doc/c-api/long.rst
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ Long Integer Objects
Return a C :c:type:`unsigned long` from a Python long integer, without checking
for overflow.

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

.. versionadded:: 2.3


Expand All @@ -225,6 +228,9 @@ Long Integer Objects
Return a C :c:type:`unsigned long long` from a Python long integer, without
checking for overflow.

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

.. versionadded:: 2.3


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 @@ -888,6 +888,26 @@ test_long_long_and_overflow(PyObject *self)
return Py_None;
}

static PyObject *
test_long_as_unsigned_long_long_mask(PyObject *self)
{
unsigned PY_LONG_LONG res = PyLong_AsUnsignedLongLongMask(NULL);

if (res != (unsigned PY_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_INCREF(Py_None);
return Py_None;
}

/* Test the L code for PyArg_ParseTuple. This should deliver a PY_LONG_LONG
for both long and int arguments. The test may leak a little memory if
it fails.
Expand Down Expand Up @@ -2715,6 +2735,8 @@ static PyMethodDef TestMethods[] = {
{"test_longlong_api", test_longlong_api, METH_NOARGS},
{"test_long_long_and_overflow",
(PyCFunction)test_long_long_and_overflow, METH_NOARGS},
{"test_long_as_unsigned_long_long_mask",
(PyCFunction)test_long_as_unsigned_long_long_mask, METH_NOARGS},
{"test_L_code", (PyCFunction)test_L_code, METH_NOARGS},
#endif
{"getargs_f", getargs_f, METH_VARARGS},
Expand Down
2 changes: 1 addition & 1 deletion Objects/longobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,7 @@ PyLong_AsUnsignedLongLongMask(PyObject *vv)

if (vv == NULL || !PyLong_Check(vv)) {
PyErr_BadInternalCall();
return (unsigned long) -1;
return (unsigned PY_LONG_LONG) -1;
}
v = (PyLongObject *)vv;
i = Py_SIZE(v);
Expand Down