chiark / gitweb /
*.c: Be more careful about `PySequence_Size'.
authorMark Wooding <mdw@distorted.org.uk>
Sun, 24 Nov 2019 16:36:24 +0000 (16:36 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Wed, 27 Nov 2019 15:10:44 +0000 (15:10 +0000)
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.

algorithms.c
catacomb.c
ec.c
mp.c

index d60fe3efb87e5b46e99b56b3c33c20309944ee17..edd73fd8119224577430b7aa7062bc990a58453c 100644 (file)
@@ -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");
   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);
   if (dfl < 0) VALERR("key size cannot be negative");
   x = PyInt_FromLong(dfl);
   PyList_Append(l, x);
index daa404a4e29620dc748442d580c765468c3295dc..e24e5ec9f47e033f27129b1b002da90a2b68c273 100644 (file)
@@ -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");
     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;
   x = PySequence_GetItem(arg, 0);
   if (PySequence_Check(x))
     flat = 0;
diff --git a/ec.c b/ec.c
index ef5d855a9c7071dfc69fd947a4a9e14da96305fe..6280010d7701421026de9589f75fc77206aa3691 100644 (file)
--- 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;
     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 ||
     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 f2a00b996047ae91d7ba20a794dfcb5f325cf04c..ab15d3fd10e115c476f1623e1eb162c37bb1d68f 100644 (file)
--- 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");
     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;
   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");
     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++) {
   if (!n) VALERR("want at least one modulus");
   v = xmalloc(n * sizeof(*v));
   for (i = 0; i < n; i++) {