From: zooko Date: Wed, 28 Mar 2007 06:53:32 +0000 (+0530) Subject: pyfec: correctly handle wrongly typed input by raising exception X-Git-Url: https://git.rkrishnan.org/%5B/%5D%20/uri/%22doc.html/rgr-080307.php?a=commitdiff_plain;h=a45393a914e51c19ec2b32ae76dacd0fa3789bce;p=tahoe-lafs%2Fzfec.git pyfec: correctly handle wrongly typed input by raising exception darcs-hash:a264294247306af6ca2577668fcda8df5e34551f --- 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; imm - 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= 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; ikk; 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()