From a45393a914e51c19ec2b32ae76dacd0fa3789bce Mon Sep 17 00:00:00 2001
From: zooko <zooko@zooko.com>
Date: Wed, 28 Mar 2007 12:23:32 +0530
Subject: [PATCH] pyfec: correctly handle wrongly typed input by raising
 exception

darcs-hash:a264294247306af6ca2577668fcda8df5e34551f
---
 pyfec/fec/_fecmodule.c       | 12 ++++++++----
 pyfec/fec/test/test_pyfec.py | 23 ++++++++++++++++++++++-
 2 files changed, 30 insertions(+), 5 deletions(-)

diff --git a/pyfec/fec/_fecmodule.c b/pyfec/fec/_fecmodule.c
index a3497c8..66a6448 100644
--- a/pyfec/fec/_fecmodule.c
+++ b/pyfec/fec/_fecmodule.c
@@ -151,13 +151,17 @@ Encoder_encode(Encoder *self, PyObject *args) {
     unsigned c_desired_shares_ids[self->mm];
     unsigned c_desired_checkshares_ids[self->mm - self->kk];
     unsigned i;
+    for (i=0; i<self->mm - self->kk; i++)
+        pystrs_produced[i] = NULL;
     if (desired_shares_ids) {
         fast_desired_shares_ids = PySequence_Fast(desired_shares_ids, "Second argument (optional) was not a sequence.");
         num_desired_shares = PySequence_Fast_GET_SIZE(fast_desired_shares_ids);
         fast_desired_shares_ids_items = PySequence_Fast_ITEMS(fast_desired_shares_ids);
         for (i=0; i<num_desired_shares; i++) {
-            if (!PyInt_Check(fast_desired_shares_ids_items[i]))
+            if (!PyInt_Check(fast_desired_shares_ids_items[i])) {
+                py_raise_fec_error("Precondition violation: second argument is required to contain int.");
                 goto err;
+            }
             c_desired_shares_ids[i] = PyInt_AsLong(fast_desired_shares_ids_items[i]);
             if (c_desired_shares_ids[i] >= self->kk)
                 num_check_shares_produced++;
@@ -169,8 +173,6 @@ Encoder_encode(Encoder *self, PyObject *args) {
         num_check_shares_produced = self->mm - self->kk;
     }
 
-    for (i=0; i<num_check_shares_produced; i++)
-        pystrs_produced[i] = NULL;
     PyObject* fastinshares = PySequence_Fast(inshares, "First argument was not a sequence.");
     if (!fastinshares)
         goto err;
@@ -426,8 +428,10 @@ Decoder_decode(Decoder *self, PyObject *args) {
         goto err;
     Py_ssize_t sz, oldsz = 0;
     for (i=0; i<self->kk; i++) {
-        if (!PyInt_Check(fastshareidsitems[i]))
+        if (!PyInt_Check(fastshareidsitems[i])) {
+            py_raise_fec_error("Precondition violation: second argument is required to contain int.");
             goto err;
+        }
         long tmpl = PyInt_AsLong(fastshareidsitems[i]);
         if (tmpl < 0 || tmpl > 255) {
             py_raise_fec_error("Precondition violation: Share ids can't be less than zero or greater than 255.  %ld\n", tmpl);
diff --git a/pyfec/fec/test/test_pyfec.py b/pyfec/fec/test/test_pyfec.py
index 8e84357..d046c51 100755
--- a/pyfec/fec/test/test_pyfec.py
+++ b/pyfec/fec/test/test_pyfec.py
@@ -92,12 +92,33 @@ def _test_random():
     _h(k, m, ss)
 
 def test_random():
-    for i in range(2**7):
+    for i in range(2**5):
         # sys.stdout.write(",")
         _test_random()
         # sys.stdout.write(".")
     print "%d randomized tests pass." % (i+1)
 
+def test_bad_args_enc():
+    encer = fec.Encoder(2, 4)
+    try:
+        encer.encode(["a", "b", ], ["c", "d",])
+    except fec.Error, e:
+        assert "econd argument is required to contain int" in str(e), e
+    else:
+        raise "Should have gotten fec.Error for wrong type of second argument."
+
+def test_bad_args_dec():
+    decer = fec.Decoder(2, 4)
+    try:
+        decer.decode(["a", "b", ], ["c", "d",])
+    except fec.Error, e:
+        assert "econd argument is required to contain int" in str(e), e
+    else:
+        raise "Should have gotten fec.Error for wrong type of second argument."
+    
+
 if __name__ == "__main__":
+    test_bad_args_dec()
+    test_bad_args_enc()
     test_random()
 
-- 
2.45.2