From: Zooko O'Whielacronx Date: Wed, 28 Mar 2007 06:53:32 +0000 (-0700) Subject: pyfec: correctly handle wrongly typed input by raising exception X-Git-Url: https://git.rkrishnan.org/%5B/frontends/%22news.html/%22doc.html/cyclelanguage?a=commitdiff_plain;h=4e86a6df76662e5559304f3bbb60d36fbd2258d5;p=tahoe-lafs%2Ftahoe-lafs.git pyfec: correctly handle wrongly typed input by raising exception --- diff --git a/pyfec/fec/_fecmodule.c b/pyfec/fec/_fecmodule.c index a3497c8b..66a64485 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 8e843570..d046c514 100644 --- 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()