chiark / gitweb /
*.c: Be more careful about `PySequence_Size'.
[catacomb-python] / mp.c
diff --git a/mp.c b/mp.c
index bad566d7166687362ff9a3e8cfe529cb79a0a887..ab15d3fd10e115c476f1623e1eb162c37bb1d68f 100644 (file)
--- a/mp.c
+++ b/mp.c
@@ -211,6 +211,7 @@ mp *tomp(PyObject *o)
     return (MP_COPY(PFILT_F(o)->m));
   else if (ECPT_PYCHECK(o)) {
     ec p = EC_INIT;
+    if (EC_ATINF(ECPT_P(o))) return (0);
     getecptout(&p, o);
     x = MP_COPY(p.x);
     EC_DESTROY(&p);
@@ -1679,7 +1680,7 @@ static PyObject *mcmeth_solve(PyObject *me, PyObject *arg)
   PyObject *q = 0, *x, *z = 0;
   mp *xx;
   mp **v = 0;
-  int i = 0, n = c->k;
+  Py_ssize_t i = 0, n = c->k;
 
   Py_INCREF(me);
   if (PyTuple_Size(arg) == n)
@@ -1688,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;
@@ -1718,7 +1720,7 @@ static void mpcrt_pydealloc(PyObject *me)
 static PyObject *mpcrt_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
 {
   mpcrt_mod *v = 0;
-  int n, i = 0, j;
+  Py_ssize_t n, i = 0, j;
   char *kwlist[] = { "mv", 0 };
   PyObject *q = 0, *x;
   mp *xx = MP_NEW, *y = MP_NEW, *g = MP_NEW;
@@ -1731,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++) {
@@ -2369,8 +2370,9 @@ static PyObject *gfn_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
                                   convgf, &p, convgf, &beta))
     goto end;
   gg = PyObject_New(gfn_pyobj, ty);
+  gg->p = 0;
   if (gfn_create(p, beta, &gg->ntop, &gg->pton)) {
-    FREEOBJ(gg);
+    Py_DECREF(gg);
     gg = 0;
     VALERR("can't invert transformation matrix");
   }
@@ -2402,7 +2404,7 @@ static PyObject *gfnget_beta(PyObject *me, void *hunoz)
   end:                                                                 \
     mp_drop(xx);                                                       \
     if (!z) return (0);                                                        \
-    return (mp_pywrap(z));                                             \
+    return (gf_pywrap(z));                                             \
   }
 XFORMOP(pton, PTON)
 XFORMOP(ntop, NTOP)
@@ -2410,8 +2412,11 @@ XFORMOP(ntop, NTOP)
 
 static void gfn_pydealloc(PyObject *me)
 {
-  gfn_destroy(GFN_PTON(me));
-  gfn_destroy(GFN_NTOP(me));
+  if (GFN_P(me)) {
+    MP_DROP(GFN_P(me));
+    gfn_destroy(GFN_PTON(me));
+    gfn_destroy(GFN_NTOP(me));
+  }
   FREEOBJ(me);
 }