Fix a potential divide-by-zero error. miscellaneous-fixes
authorachurch
Fri, 07 Jan 2011 02:36:27 +0900
branchmiscellaneous-fixes
changeset 639 3e9ea3275214
parent 634 a65d3f91b17f
child 640 57445129408f
child 652 8cc6d08d2392
Fix a potential divide-by-zero error. This patch corrects a case in which divide-by-zero could occur, resulting in a crash on some systems (such as when first entering the Veil area, as reported by Tom Lu).
BBGE/Base.cpp
--- a/BBGE/Base.cpp	Thu Jan 06 01:31:07 2011 +0900
+++ b/BBGE/Base.cpp	Fri Jan 07 02:36:27 2011 +0900
@@ -729,12 +729,17 @@
 {
     Vector dir = lineEnd - lineStart;
     Vector diff = point - lineStart;
-    float t = diff.dot2D(dir) / dir.dot2D(dir);
-    if (t < 0.0f)
-        t = 0.0f;
-    if (t > 1.0f)
-        t = 1.0f;
-    Vector closest = lineStart + t * dir;
+    Vector closest;
+    if (!dir.isZero()) {
+	float t = diff.dot2D(dir) / dir.dot2D(dir);
+	if (t < 0.0f)
+	    t = 0.0f;
+	if (t > 1.0f)
+	    t = 1.0f;
+	closest = lineStart + t * dir;
+    } else {
+	closest = lineStart;
+    }
     Vector d = point - closest;
     float distsqr = d.dot2D(d);
 	if (closestP)