aboutsummaryrefslogtreecommitdiffstats
path: root/lib/mpi/mpicoder.c
diff options
context:
space:
mode:
authorNicolai Stange <nicstange@gmail.com>2016-03-22 08:12:35 -0400
committerHerbert Xu <herbert@gondor.apana.org.au>2016-04-05 08:35:46 -0400
commitf2d1362ff7d266b3d2b1c764d6c2ef4a3b457f23 (patch)
tree030e179e9fbf37af09328a1abfdccd5dd61fdaee /lib/mpi/mpicoder.c
parent3cf9d84eb84abb4c186cd793d8720437a2ee2b1a (diff)
lib/mpi: mpi_write_sgl(): fix skipping of leading zero limbs
Currently, if the number of leading zeros is greater than fits into a complete limb, mpi_write_sgl() skips them by iterating over them limb-wise. However, it fails to adjust its internal leading zeros tracking variable, lzeros, accordingly: it does a p -= sizeof(alimb); continue; which should really have been a lzeros -= sizeof(alimb); continue; Since lzeros never decreases if its initial value >= sizeof(alimb), nothing gets copied by mpi_write_sgl() in that case. Instead of skipping the high order zero limbs within the loop as shown above, fix the issue by adjusting the copying loop's bounds. Fixes: 2d4d1eea540b ("lib/mpi: Add mpi sgl helpers") Signed-off-by: Nicolai Stange <nicstange@gmail.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'lib/mpi/mpicoder.c')
-rw-r--r--lib/mpi/mpicoder.c21
1 files changed, 9 insertions, 12 deletions
diff --git a/lib/mpi/mpicoder.c b/lib/mpi/mpicoder.c
index eb15e7dc7b65..6bb52beb06b0 100644
--- a/lib/mpi/mpicoder.c
+++ b/lib/mpi/mpicoder.c
@@ -380,7 +380,9 @@ int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned *nbytes,
380 buf_len = sgl->length; 380 buf_len = sgl->length;
381 p2 = sg_virt(sgl); 381 p2 = sg_virt(sgl);
382 382
383 for (i = a->nlimbs - 1; i >= 0; i--) { 383 for (i = a->nlimbs - 1 - lzeros / BYTES_PER_MPI_LIMB,
384 lzeros %= BYTES_PER_MPI_LIMB;
385 i >= 0; i--) {
384 alimb = a->d[i]; 386 alimb = a->d[i];
385 p = (u8 *)&alimb2; 387 p = (u8 *)&alimb2;
386#if BYTES_PER_MPI_LIMB == 4 388#if BYTES_PER_MPI_LIMB == 4
@@ -401,17 +403,12 @@ int mpi_write_to_sgl(MPI a, struct scatterlist *sgl, unsigned *nbytes,
401#error please implement for this limb size. 403#error please implement for this limb size.
402#endif 404#endif
403 if (lzeros > 0) { 405 if (lzeros > 0) {
404 if (lzeros >= sizeof(alimb)) { 406 mpi_limb_t *limb1 = (void *)p - sizeof(alimb);
405 p -= sizeof(alimb); 407 mpi_limb_t *limb2 = (void *)p - sizeof(alimb)
406 continue; 408 + lzeros;
407 } else { 409 *limb1 = *limb2;
408 mpi_limb_t *limb1 = (void *)p - sizeof(alimb); 410 p -= lzeros;
409 mpi_limb_t *limb2 = (void *)p - sizeof(alimb) 411 y = lzeros;
410 + lzeros;
411 *limb1 = *limb2;
412 p -= lzeros;
413 y = lzeros;
414 }
415 lzeros -= sizeof(alimb); 412 lzeros -= sizeof(alimb);
416 } 413 }
417 414