lib: gcd: prevent possible div by 0
PATCH 1/1 lib: gcd: prevent possible div by 0
Account for properties when a and/or b are 0:gcd(0, 0) = 0gcd(a, 0) = agcd(0, b) = b
--- lib/gcd.c | 3 +++ 1 file changed, 3 insertions(+)
lib/gcd.c +3 -0
@@ -7,6 +7,9 @@ unsigned long gcd(unsigned long a, unsigned long b) {unsigned long r; +if (!a || !b) +return a | b; +if (a < b)swap (a, b);while ((r = a % b) !=0 ) {
Signature · 1 line
1.7.9.5
Quoted 16 lines · Davidlohr Bueso
Account for properties when a and/or b are 0:
gcd(0, 0)= 0gcd(a, 0)= agcd(0, b)= bSigned-off-by Davidlohr Bueso <[email protected]>--- lib/gcd.c | 3 +++ 1 file changed, 3 insertions(+)lib/gcd.c +0
-0
lib/gcd.c unsigned long gcd(unsigned long a, unsigned long b) · line 11
Quoted 5 lines
lib/gcd.c +2
-0@@ -7,6 +7,9 @@ unsigned long gcd(unsigned long a, unsigned long b) {unsigned long r; +if (!a || !b) +return a | b;
This seems overkill
lib/gcd.c unsigned long gcd(unsigned long a, unsigned long b) · line 14
Quoted 3 lines
lib/gcd.c +1
-0+if (a < b)swap (a, b);
better here to :
if (!b) return a;
Quoted 1 line
lib/gcd.c +0
-0while ((r = a % b) !=0 ) {
Quoted 20 lines · Eric Dumazet
Quoted 19 lines · Davidlohr Bueso
Account for properties when a and/or b are 0:
gcd(0, 0)= 0gcd(a, 0)= agcd(0, b)= bSigned-off-by Davidlohr Bueso <[email protected]>--- lib/gcd.c | 3 +++ 1 file changed, 3 insertions(+)lib/gcd.c +0
-0@@ -7,6 +7,9 @@ unsigned long gcd(unsigned long a, unsigned long b) {unsigned long r;
Quoted 4 lines
lib/gcd.c unsigned long gcd(unsigned long a, unsigned long b) · line 11
Quoted 2 lines
lib/gcd.c +2
-0+if (!a || !b) +return a | b;This seems overkill
It might, but it reads better, IMHO.
Quoted 9 lines
lib/gcd.c unsigned long gcd(unsigned long a, unsigned long b) · line 14
Quoted 4 lines
lib/gcd.c +1
-0+if (a < b)swap (a, b);better here to :
if (!b) return a;
Sure, I don't mind either way. I'll send a v2 shortly.
Thanks for reviewing.
Davidlohr