ec.c: Don't lose error status when constructing points from a sequence.
[catacomb-python] / ec.c
diff --git a/ec.c b/ec.c
index e18cbb8..361bbf5 100644 (file)
--- a/ec.c
+++ b/ec.c
@@ -252,7 +252,7 @@ static PyObject *epmeth_tobuf(PyObject *me, PyObject *arg)
   if (EC_ATINF(&p))
     n = 2;
   else
-    n = mp_octets(p.x) + mp_octets(p.y) + 4;
+    n = mp_octets(p.x) + mp_octets(p.y) + 6;
   rc = bytestring_pywrap(0, n);
   buf_init(&b, PyString_AS_STRING(rc), n);
   buf_putec(&b, &p);
@@ -453,7 +453,7 @@ static int ecptxl_1(ec_curve *c, ec *p, PyObject *x)
     getecptout(p, x);
     goto fix;
   } else if (PyString_Check(x)) {
-    if (PyObject_AsReadBuffer(x, &q, 0))
+    if (PyObject_AsReadBuffer(x, &q, &n))
       goto end;
     qd.p = q;
     qd.e = 0;
@@ -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 ||
@@ -473,6 +473,7 @@ static int ecptxl_1(ec_curve *c, ec *p, PyObject *x)
        (n == 3 && (z = PySequence_GetItem(t, 2)) == 0))
       goto end;
     rc = (n == 2) ? ecptxl_2(c, p, x, y) : ecptxl_3(c, p, x, y, z);
+    goto end;
   } else
     TYERR("can't convert to curve point");
   goto ok;
@@ -508,7 +509,7 @@ static PyObject *ecptnc_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
     goto end;
   return (ecpt_pywrapout(ty, &p));
 end:
-  EC_DESTROY(&p);
+  mp_drop(p.x); mp_drop(p.y); mp_drop(p.z);
   return (0);
 }
 
@@ -550,7 +551,7 @@ static PyObject *ecpt_pynew(PyTypeObject *ty, PyObject *arg, PyObject *kw)
     goto end;
   return (ecpt_pywrap((PyObject *)ty, &p));
 end:
-  EC_DESTROY(&p);
+  mp_drop(p.x); mp_drop(p.y); mp_drop(p.z);
   return (0);
 }