From 65247e595d8e67ebc2857a8c3bb5ed53893b500d Mon Sep 17 00:00:00 2001 From: Cary Clark Date: Tue, 13 Mar 2018 10:39:30 -0400 Subject: avoid divide by zero R=kjlubick@google.com Bug: skia:7623 Change-Id: Ia9fb31201a39a4540d94f97e8d2f06eb290ab6e5 Reviewed-on: https://skia-review.googlesource.com/114082 Commit-Queue: Cary Clark Commit-Queue: Cary Clark Reviewed-by: Kevin Lubick --- src/pathops/SkPathOpsQuad.cpp | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'src/pathops') diff --git a/src/pathops/SkPathOpsQuad.cpp b/src/pathops/SkPathOpsQuad.cpp index f410ea3737..9a104bfe2b 100644 --- a/src/pathops/SkPathOpsQuad.cpp +++ b/src/pathops/SkPathOpsQuad.cpp @@ -141,6 +141,15 @@ int SkDQuad::RootsValidT(double A, double B, double C, double t[2]) { return foundRoots; } +static int handle_zero(const double B, const double C, double s[2]) { + if (approximately_zero(B)) { + s[0] = 0; + return C == 0; + } + s[0] = -C / B; + return 1; +} + /* Numeric Solutions (5.6) suggests to solve the quadratic by computing Q = -1/2(B + sgn(B)Sqrt(B^2 - 4 A C)) @@ -150,16 +159,13 @@ and using the roots */ // this does not discard real roots <= 0 or >= 1 int SkDQuad::RootsReal(const double A, const double B, const double C, double s[2]) { + if (!A) { + return handle_zero(B, C, s); + } const double p = B / (2 * A); const double q = C / A; - if (!A || (approximately_zero(A) && (approximately_zero_inverse(p) - || approximately_zero_inverse(q)))) { - if (approximately_zero(B)) { - s[0] = 0; - return C == 0; - } - s[0] = -C / B; - return 1; + if (approximately_zero(A) && (approximately_zero_inverse(p) || approximately_zero_inverse(q))) { + return handle_zero(B, C, s); } /* normal form: x^2 + px + q = 0 */ const double p2 = p * p; -- cgit v1.2.3