From: Zooko O'Whielacronx zooko@zooko.com Date: Wed, 24 Jan 2007 21:17:51 +0000 (+0530) Subject: stricter typing -- using unsigned char for indexes into shares X-Git-Url: https://git.rkrishnan.org/components/com_hotproperty/cyclelanguage?a=commitdiff_plain;h=8026f6279c2e522dfd1f64d1300d086771314814;p=tahoe-lafs%2Fzfec.git stricter typing -- using unsigned char for indexes into shares darcs-hash:37229957b23f14b86e659f3f359c2c3d20c5469e --- diff --git a/pyfec/fec/fec.c b/pyfec/fec/fec.c index b5cf444..fcedb16 100644 --- a/pyfec/fec/fec.c +++ b/pyfec/fec/fec.c @@ -510,8 +510,8 @@ fec_free (fec_t *p) { * the encoding matrix. */ fec_t * -fec_new (int k, int n) { - int row, col; +fec_new (unsigned char k, unsigned char n) { + unsigned char row, col; gf *p, *tmp_m; fec_t *retval; @@ -521,10 +521,6 @@ fec_new (int k, int n) { if (fec_initialized == 0) init_fec (); - if (k < 1 || k > 256 || n > 256 || k > n) { - ERR("Invalid parameters k %d n %d GF_SIZE %d", k, n, 255); - return NULL; - } retval = (fec_t *) my_malloc (sizeof (fec_t), "new_code"); retval->k = k; retval->n = n; @@ -605,7 +601,7 @@ fec_encode_all(const fec_t* code, const gf*restrict const*restrict const src, gf * @param matrix a space allocated for a k by k matrix */ void -build_decode_matrix_into_space(const fec_t*restrict const code, const int*const restrict index, const int k, gf*restrict const matrix) { +build_decode_matrix_into_space(const fec_t*restrict const code, const unsigned char*const restrict index, const unsigned char k, gf*restrict const matrix) { unsigned i; gf* p; for (i=0, p=matrix; i < k; i++, p += k) { @@ -620,7 +616,7 @@ build_decode_matrix_into_space(const fec_t*restrict const code, const int*const } void -fec_decode_all(const fec_t* code, const gf*restrict const*restrict const inpkts, gf*restrict const*restrict const outpkts, const unsigned*restrict const index, unsigned sz) { +fec_decode_all(const fec_t* code, const gf*restrict const*restrict const inpkts, gf*restrict const*restrict const outpkts, const unsigned char*restrict const index, size_t sz) { gf m_dec[code->k * code->k]; build_decode_matrix_into_space(code, index, code->k, m_dec); diff --git a/pyfec/fec/fec.h b/pyfec/fec/fec.h index 43adcef..42b103d 100644 --- a/pyfec/fec/fec.h +++ b/pyfec/fec/fec.h @@ -52,7 +52,7 @@ typedef struct { } fec_t; void fec_free (fec_t *p); -fec_t *fec_new (int k, int n); +fec_t *fec_new (unsigned char k, unsigned char n); /** * @param inpkts the "primary shares" i.e. the chunks of the input data @@ -68,6 +68,6 @@ void fec_encode_all(const fec_t* code, const gf*restrict const*restrict const sr * @param index an array of the shareids of the packets in inpkts * @param sz size of a packet in bytes */ -void fec_decode_all(const fec_t* code, const gf*restrict const*restrict const inpkts, gf*restrict const*restrict const outpkts, const unsigned*restrict const index, unsigned sz); +void fec_decode_all(const fec_t* code, const gf*restrict const*restrict const inpkts, gf*restrict const*restrict const outpkts, const unsigned char*restrict const index, size_t sz); /* end of file */ diff --git a/pyfec/fec/fecmodule.c b/pyfec/fec/fecmodule.c index f8ba4f0..50dc695 100644 --- a/pyfec/fec/fecmodule.c +++ b/pyfec/fec/fecmodule.c @@ -340,7 +340,7 @@ Decoder_decode(Decoder *self, PyObject *args) { return NULL; const gf*restrict cshares[self->kk]; - unsigned csharenums[self->kk]; + unsigned char csharenums[self->kk]; gf*restrict recoveredcstrs[self->kk]; /* self->kk is actually an upper bound -- we probably won't need all of this space. */ PyObject*restrict recoveredpystrs[self->kk]; /* self->kk is actually an upper bound -- we probably won't need all of this space. */ unsigned i; @@ -374,7 +374,12 @@ Decoder_decode(Decoder *self, PyObject *args) { for (i=0; ikk; i++) { if (!PyInt_Check(fastsharenumsitems[i])) goto err; - csharenums[i] = PyInt_AsLong(fastsharenumsitems[i]); + long tmpl = PyInt_AsLong(fastsharenumsitems[i]); + if (tmpl < 0 || tmpl >= UCHAR_MAX) { + py_raise_fec_error("Precondition violation: Share ids can't be less than zero or greater than 255. %ld\n", tmpl); + goto err; + } + csharenums[i] = (unsigned char)tmpl; if (csharenums[i] >= self->kk) needtorecover+=1; diff --git a/pyfec/fec/test/test_pyfec.py b/pyfec/fec/test/test_pyfec.py index 9eae42c..c84d0b2 100755 --- a/pyfec/fec/test/test_pyfec.py +++ b/pyfec/fec/test/test_pyfec.py @@ -33,7 +33,7 @@ def _h(k, m, ss): nums = [ x[0] for x in nums_and_shares ] # sys.stdout.write("about to construct Decoder.\n") ; sys.stdout.flush() decer = fec.Decoder(k, m) - # sys.stdout.write("about to decode.\n") ; sys.stdout.flush() + # sys.stdout.write("about to decode from %s.\n"%nums) ; sys.stdout.flush() decoded = decer.decode(shares, nums) # sys.stdout.write("decoded.\n") ; sys.stdout.flush() _assert(len(decoded) == len(ss), len(decoded), len(ss)) @@ -64,17 +64,14 @@ def pad_size(n, k): return 0 def _test_random(): - # m = random.randrange(1, 255) - m = 99 - # k = random.randrange(1, m+1) - k = 33 - # l = random.randrange(0, 2**16) - l = 2**12 + m = random.randrange(1, 255) + k = random.randrange(1, m+1) + l = random.randrange(0, 2**16) ss = [ randstr(l/k) + '\x00' * pad_size(l/k, k) for x in range(k) ] _h(k, m, ss) def test_random(): - for i in range(2**9): + for i in range(2**10): sys.stdout.write(",") _test_random() sys.stdout.write(".")