pyfec: correctly handle wrongly typed input by raising exception
authorzooko <zooko@zooko.com>
Wed, 28 Mar 2007 06:53:32 +0000 (12:23 +0530)
committerzooko <zooko@zooko.com>
Wed, 28 Mar 2007 06:53:32 +0000 (12:23 +0530)
darcs-hash:a264294247306af6ca2577668fcda8df5e34551f

pyfec/fec/_fecmodule.c
pyfec/fec/test/test_pyfec.py

index a3497c8bb3a169ac6bfbdf6d79362a5c8904c154..66a64485317ed179a8e1ca1594488c89d3c52897 100644 (file)
@@ -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);
index 8e8435702bfdb402bfd974d37d02480b90fe9eb6..d046c514bb364b4aa83e8b3a9bbef3d4caac3ff4 100755 (executable)
@@ -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()