pyfec: correctly handle wrongly typed input by raising exception

This commit is contained in:
Zooko O'Whielacronx 2007-03-27 23:53:32 -07:00
parent e4463056f3
commit 4e86a6df76
2 changed files with 30 additions and 5 deletions

View 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);

View File

@ -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()