From: Mark Wooding Date: Sun, 24 Nov 2019 16:36:24 +0000 (+0000) Subject: *.c: Be more careful about `PySequence_Size'. X-Git-Url: https://git.distorted.org.uk/~mdw/catacomb-python/commitdiff_plain/4281a7ee8646165a39f03bcab908b30dee643dae *.c: Be more careful about `PySequence_Size'. This can be implemented by Python, so it can throw exceptions. Fortunately, Python checks that the result is nonnegative, so we don't have to worry about that. --- diff --git a/algorithms.c b/algorithms.c index d60fe3e..edd73fd 100644 --- a/algorithms.c +++ b/algorithms.c @@ -138,9 +138,8 @@ static PyObject *keyszset_pynew(PyTypeObject *ty, if (!set) set = PyTuple_New(0); else Py_INCREF(set); if (!PySequence_Check(set)) TYERR("want a sequence"); - n = PySequence_Size(set); - l = PyList_New(0); - if (PyErr_Occurred()) goto end; + n = PySequence_Size(set); if (n < 0) goto end; + l = PyList_New(0); if (!l) goto end; if (dfl < 0) VALERR("key size cannot be negative"); x = PyInt_FromLong(dfl); PyList_Append(l, x); diff --git a/catacomb.c b/catacomb.c index daa404a..e24e5ec 100644 --- a/catacomb.c +++ b/catacomb.c @@ -76,7 +76,8 @@ PyObject *mexp_common(PyObject *me, PyObject *arg, arg = PyTuple_GetItem(arg, 0); Py_INCREF(arg); if (!PySequence_Check(arg)) TYERR("not a sequence"); - n = PySequence_Size(arg); if (!n) { z = id(me); goto end; } + n = PySequence_Size(arg); if (n < 0) goto end; + if (!n) { z = id(me); goto end; } x = PySequence_GetItem(arg, 0); if (PySequence_Check(x)) flat = 0; diff --git a/ec.c b/ec.c index ef5d855..6280010 100644 --- a/ec.c +++ b/ec.c @@ -465,7 +465,7 @@ static int ecptxl_1(ec_curve *c, ec *p, PyObject *x) if (!EC_FIND(c, p, xx)) VALERR("not on the curve"); } else if (PySequence_Check(x)) { t = x; x = 0; - n = PySequence_Size(t); + n = PySequence_Size(t); if (n < 0) goto end; if (n != 2 && (n != 3 || !c)) TYERR("want sequence of two or three items"); if ((x = PySequence_GetItem(t, 0)) == 0 || diff --git a/mp.c b/mp.c index f2a00b9..ab15d3f 100644 --- a/mp.c +++ b/mp.c @@ -1689,7 +1689,8 @@ static PyObject *mcmeth_solve(PyObject *me, PyObject *arg) goto end; Py_INCREF(q); if (!PySequence_Check(q)) TYERR("want a sequence of residues"); - if (PySequence_Size(q) != n) VALERR("residue count mismatch"); + i = PySequence_Size(q); if (i < 0) goto end; + if (i != n) VALERR("residue count mismatch"); v = xmalloc(n * sizeof(*v)); for (i = 0; i < n; i++) { if ((x = PySequence_GetItem(q, i)) == 0) goto end; @@ -1732,8 +1733,7 @@ static PyObject *mpcrt_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw) goto end; Py_INCREF(q); if (!PySequence_Check(q)) TYERR("want a sequence of moduli"); - n = PySequence_Size(q); - if (PyErr_Occurred()) goto end; + n = PySequence_Size(q); if (n < 0) goto end; if (!n) VALERR("want at least one modulus"); v = xmalloc(n * sizeof(*v)); for (i = 0; i < n; i++) {