pyfec: add precondition checks on the values of k and m to constructors

This commit is contained in:
Zooko O'Whielacronx 2007-01-25 18:47:04 -07:00
parent 3e6ef757d3
commit f62c5f9c5d
1 changed files with 33 additions and 8 deletions

View File

@ -96,11 +96,23 @@ Encoder_init(Encoder *self, PyObject *args, PyObject *kwdict) {
if (!PyArg_ParseTupleAndKeywords(args, kwdict, "ii", kwlist, &self->kk, &self->mm)) if (!PyArg_ParseTupleAndKeywords(args, kwdict, "ii", kwlist, &self->kk, &self->mm))
return -1; return -1;
self->fec_matrix = fec_new(self->kk, self->mm); if (self->kk < 1) {
if(self->fec_matrix == NULL) { py_raise_fec_error("Precondition violation: first argument is required to be greater than or equal to 1, but it was %d", self->kk);
py_raise_fec_error(fec_error); /* xyz */ return -1;
return -1;
} }
if (self->mm < 1) {
py_raise_fec_error("Precondition violation: second argument is required to be greater than or equal to 1, but it was %d", self->mm);
return -1;
}
if (self->mm > 255) {
py_raise_fec_error("Precondition violation: second argument is required to be less than or equal to 255, but it was %d", self->mm);
return -1;
}
if (self->kk > self->mm) {
py_raise_fec_error("Precondition violation: first argument is required to be less than or equal to the second argument, but they were %d and %d respectively", self->kk, self->mm);
return -1;
}
self->fec_matrix = fec_new(self->kk, self->mm);
return 0; return 0;
} }
@ -333,14 +345,27 @@ Decoder_init(Encoder *self, PyObject *args, PyObject *kwdict) {
"m", "m",
NULL NULL
}; };
if (!PyArg_ParseTupleAndKeywords(args, kwdict, "ii", kwlist, &self->kk, &self->mm)) if (!PyArg_ParseTupleAndKeywords(args, kwdict, "ii", kwlist, &self->kk, &self->mm))
return -1; return -1;
self->fec_matrix = fec_new(self->kk, self->mm); if (self->kk < 1) {
if(self->fec_matrix == NULL) { py_raise_fec_error("Precondition violation: first argument is required to be greater than or equal to 1, but it was %d", self->kk);
py_raise_fec_error(fec_error); /* xyz */ return -1;
return -1;
} }
if (self->mm < 1) {
py_raise_fec_error("Precondition violation: second argument is required to be greater than or equal to 1, but it was %d", self->mm);
return -1;
}
if (self->mm > 255) {
py_raise_fec_error("Precondition violation: second argument is required to be less than or equal to 255, but it was %d", self->mm);
return -1;
}
if (self->kk > self->mm) {
py_raise_fec_error("Precondition violation: first argument is required to be less than or equal to the second argument, but they were %d and %d respectively", self->kk, self->mm);
return -1;
}
self->fec_matrix = fec_new(self->kk, self->mm);
return 0; return 0;
} }