lib/lcm.c: ensure correct result whenever it fits
authorRasmus Villemoes <linux@rasmusvillemoes.dk>
Wed, 10 Dec 2014 23:51:27 +0000 (15:51 -0800)
committerLinus Torvalds <torvalds@linux-foundation.org>
Thu, 11 Dec 2014 01:41:11 +0000 (17:41 -0800)
Ensure that lcm(a,b) returns the mathematically correct result, provided
it fits in an unsigned long.  The current version returns garbage if a*b
overflows, even if the final result would fit.

Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: Martin K. Petersen <martin.petersen@oracle.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
lib/lcm.c

index b9c8de461e9e1630159b2edfd21a4ca49669a63c..01b3aa922dda39ee1eec7e37e285c1d1339ce910 100644 (file)
--- a/lib/lcm.c
+++ b/lib/lcm.c
@@ -7,7 +7,7 @@
 unsigned long lcm(unsigned long a, unsigned long b)
 {
        if (a && b)
-               return (a * b) / gcd(a, b);
+               return (a / gcd(a, b)) * b;
        else if (b)
                return b;