factor: catch integer overflow.
authorRob Landley <rob@landley.net>
Fri, 1 Aug 2014 23:50:46 +0000 (18:50 -0500)
committerRob Landley <rob@landley.net>
Fri, 1 Aug 2014 23:50:46 +0000 (18:50 -0500)
Now factor 9223372036854775783 (largest positive 64 bit signed prime) takes a
couple minutes but gives the right answer.

toys/other/factor.c

index e3992a8..9dc1cca 100644 (file)
@@ -49,7 +49,9 @@ static void factor(char *s)
 
   // test odd numbers.
   for (ll=3; ;ll += 2) {
-    if (ll*ll>l) {
+    long lll = ll*ll;
+
+    if (lll>l || lll<ll) {
       if (l>1) printf(" %ld", l);
       break;
     }