Skip to content

Commit

Permalink
lib/gcd.c: prevent possible div by 0
Browse files Browse the repository at this point in the history
Account for all properties when a and/or b are 0:
gcd(0, 0) = 0
gcd(a, 0) = a
gcd(0, b) = b

Fixes no known problems in current kernels.

Signed-off-by: Davidlohr Bueso <[email protected]>
Cc: Eric Dumazet <[email protected]>
Cc: <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
Davidlohr Bueso authored and torvalds committed Oct 5, 2012
1 parent 8f1f66e commit e968756
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/gcd.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ unsigned long gcd(unsigned long a, unsigned long b)

if (a < b)
swap(a, b);

if (!b)
return a;
while ((r = a % b) != 0) {
a = b;
b = r;
Expand Down

0 comments on commit e968756

Please sign in to comment.