Skip to content
Merged
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
26 changes: 16 additions & 10 deletions Modules/mathmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3056,6 +3056,12 @@ math_perm_impl(PyObject *module, PyObject *n, PyObject *k)
"n must be a non-negative integer");
goto error;
}
if (Py_SIZE(k) < 0) {
PyErr_SetString(PyExc_ValueError,
"k must be a non-negative integer");
goto error;
}

cmp = PyObject_RichCompareBool(n, k, Py_LT);
if (cmp != 0) {
if (cmp > 0) {
Expand All @@ -3072,11 +3078,8 @@ math_perm_impl(PyObject *module, PyObject *n, PyObject *k)
LLONG_MAX);
goto error;
}
else if (overflow < 0 || factors < 0) {
if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_ValueError,
"k must be a non-negative integer");
}
else if (factors == -1) {
/* k is nonnegative, so a return value of -1 can only indicate error */
goto error;
}

Expand Down Expand Up @@ -3176,6 +3179,12 @@ math_comb_impl(PyObject *module, PyObject *n, PyObject *k)
"n must be a non-negative integer");
goto error;
}
if (Py_SIZE(k) < 0) {
PyErr_SetString(PyExc_ValueError,
"k must be a non-negative integer");
goto error;
}

/* k = min(k, n - k) */
temp = PyNumber_Subtract(n, k);
if (temp == NULL) {
Expand Down Expand Up @@ -3204,11 +3213,8 @@ math_comb_impl(PyObject *module, PyObject *n, PyObject *k)
LLONG_MAX);
goto error;
}
else if (overflow < 0 || factors < 0) {
if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_ValueError,
"k must be a non-negative integer");
}
if (factors == -1) {
/* k is nonnegative, so a return value of -1 can only indicate error */
goto error;
}

Expand Down