From 7fe21291bad93458100fbbf0078d40cdb529a646 Mon Sep 17 00:00:00 2001 From: Rasmus Villemoes Date: Wed, 14 Jan 2015 15:15:57 +0000 Subject: 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(-) (limited to 'lib/mpi/mpi-cmp.c') 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); -- cgit From b9f918a31d629e99c9ca3d6ac2a2d7a905715c69 Mon Sep 17 00:00:00 2001 From: Rasmus Villemoes Date: Wed, 14 Jan 2015 16:10:12 +0000 Subject: 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(-) (limited to 'lib/mpi/mpi-cmp.c') 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); -- cgit