From 7fe21291bad93458100fbbf0078d40cdb529a646 Mon Sep 17 00:00:00 2001 From: Rasmus Villemoes Date: Wed, 14 Jan 2015 15:15:57 +0000 Subject: [PATCH 1/3] MPILIB: Deobfuscate mpi_cmp The condition preceding 'return 1;' makes my head hurt. At this point, we know that u and v have the same sign; if they are negative, they compare opposite to how their absolute values compare (which mpihelp_cmp found for us), otherwise cmp itself is the answer. Negating cmp is ok since mpihelp_cmp returns {-1,0,1}; -INT_MIN==INT_MIN won't bite us. Signed-off-by: Rasmus Villemoes Signed-off-by: David Howells Acked-by: Dmitry Kasatkin --- lib/mpi/mpi-cmp.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/mpi/mpi-cmp.c b/lib/mpi/mpi-cmp.c index 1871e7b61ca0..8ed36b8cbe02 100644 --- a/lib/mpi/mpi-cmp.c +++ b/lib/mpi/mpi-cmp.c @@ -61,10 +61,8 @@ int mpi_cmp(MPI u, MPI v) if (!usize) return 0; cmp = mpihelp_cmp(u->d, v->d, usize); - if (!cmp) - return 0; - if ((cmp < 0 ? 1 : 0) == (u->sign ? 1 : 0)) - return 1; - return -1; + if (u->sign) + return -cmp; + return cmp; } EXPORT_SYMBOL_GPL(mpi_cmp); From 98dbbcba1b0c1874b9faf9c77b83f9729360aa2c Mon Sep 17 00:00:00 2001 From: Rasmus Villemoes Date: Wed, 14 Jan 2015 15:16:00 +0000 Subject: [PATCH 2/3] MPILIB: Fix obvious but harmless typo The macro MPN_COPY_INCR this occurs in isn't used anywhere. Signed-off-by: Rasmus Villemoes Signed-off-by: David Howells --- lib/mpi/mpi-internal.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mpi/mpi-internal.h b/lib/mpi/mpi-internal.h index 60cf765628e9..c65dd1bff45a 100644 --- a/lib/mpi/mpi-internal.h +++ b/lib/mpi/mpi-internal.h @@ -84,7 +84,7 @@ static inline int RESIZE_IF_NEEDED(MPI a, unsigned b) do { \ mpi_size_t _i; \ for (_i = 0; _i < (n); _i++) \ - (d)[_i] = (d)[_i]; \ + (d)[_i] = (s)[_i]; \ } while (0) #define MPN_COPY_DECR(d, s, n) \ From b9f918a31d629e99c9ca3d6ac2a2d7a905715c69 Mon Sep 17 00:00:00 2001 From: Rasmus Villemoes Date: Wed, 14 Jan 2015 16:10:12 +0000 Subject: [PATCH 3/3] MPILIB: Fix comparison of negative MPIs If u and v both represent negative integers and their limb counts happen to differ, mpi_cmp will always return a positive value - this is obviously bogus. u is smaller than v if and only if it is larger in absolute value. Signed-off-by: Rasmus Villemoes Signed-off-by: David Howells Acked-by: Dmitry Kasatkin --- lib/mpi/mpi-cmp.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mpi/mpi-cmp.c b/lib/mpi/mpi-cmp.c index 8ed36b8cbe02..d25e9e96c310 100644 --- a/lib/mpi/mpi-cmp.c +++ b/lib/mpi/mpi-cmp.c @@ -57,7 +57,7 @@ int mpi_cmp(MPI u, MPI v) if (usize != vsize && !u->sign && !v->sign) return usize - vsize; if (usize != vsize && u->sign && v->sign) - return vsize + usize; + return vsize - usize; if (!usize) return 0; cmp = mpihelp_cmp(u->d, v->d, usize);