summaryrefslogtreecommitdiffstats
path: root/Modules/mathmodule.c
diff options
context:
space:
mode:
Diffstat (limited to 'Modules/mathmodule.c')
-rw-r--r--Modules/mathmodule.c18
1 files changed, 13 insertions, 5 deletions
diff --git a/Modules/mathmodule.c b/Modules/mathmodule.c
index 7f094ff..bce3799 100644
--- a/Modules/mathmodule.c
+++ b/Modules/mathmodule.c
@@ -1010,7 +1010,7 @@ _fsum_realloc(double **p_ptr, Py_ssize_t n,
Py_ssize_t m = *m_ptr;
m += m; /* double */
- if (n < m && m < (PY_SSIZE_T_MAX / sizeof(double))) {
+ if (n < m && (size_t)m < ((size_t)PY_SSIZE_T_MAX / sizeof(double))) {
double *p = *p_ptr;
if (p == ps) {
v = PyMem_Malloc(sizeof(double) * m);
@@ -1408,6 +1408,7 @@ static PyObject *
math_factorial(PyObject *self, PyObject *arg)
{
long x;
+ int overflow;
PyObject *result, *odd_part, *two_valuation;
if (PyFloat_Check(arg)) {
@@ -1421,15 +1422,22 @@ math_factorial(PyObject *self, PyObject *arg)
lx = PyLong_FromDouble(dx);
if (lx == NULL)
return NULL;
- x = PyLong_AsLong(lx);
+ x = PyLong_AsLongAndOverflow(lx, &overflow);
Py_DECREF(lx);
}
else
- x = PyLong_AsLong(arg);
+ x = PyLong_AsLongAndOverflow(arg, &overflow);
- if (x == -1 && PyErr_Occurred())
+ if (x == -1 && PyErr_Occurred()) {
+ return NULL;
+ }
+ else if (overflow == 1) {
+ PyErr_Format(PyExc_OverflowError,
+ "factorial() argument should not exceed %ld",
+ LONG_MAX);
return NULL;
- if (x < 0) {
+ }
+ else if (overflow == -1 || x < 0) {
PyErr_SetString(PyExc_ValueError,
"factorial() not defined for negative values");
return NULL;