progs/factorial.c: Reject negative inputs.
authorMark Wooding <mdw@distorted.org.uk>
Fri, 27 Feb 2015 14:44:45 +0000 (14:44 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Fri, 27 Feb 2015 14:46:19 +0000 (14:46 +0000)
Otherwise they get reduced mod 2^large, and the program takes forever.
It's not like we'd get a better answer by using the full-on Gamma
function, so just report an error.

progs/factorial.c

index a46debd..975c698 100644 (file)
@@ -135,7 +135,7 @@ int main(int argc, char *argv[])
   xx = mp_readstring(MP_NEW, argv[optind], &p, 0);
   while (isspace((unsigned char)*p))
     p++;
-  if (!xx || *p || MP_CMP(xx, >, ulmax))
+  if (!xx || *p || MP_CMP(xx, <, MP_ZERO) || MP_CMP(xx, >, ulmax))
     die(EXIT_FAILURE, "bad integer `%s'", argv[optind]);
   x = mp_toulong(xx);
   mp_drop(xx);