From: Mark Wooding Date: Fri, 27 Feb 2015 14:44:45 +0000 (+0000) Subject: progs/factorial.c: Reject negative inputs. X-Git-Tag: 2.2.0~15 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/catacomb/commitdiff_plain/1f14c0c645ce384406e405000375c7fb9e60cc9e?ds=inline;hp=--cc progs/factorial.c: Reject negative inputs. 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. --- 1f14c0c645ce384406e405000375c7fb9e60cc9e diff --git a/progs/factorial.c b/progs/factorial.c index a46debd1..975c698c 100644 --- a/progs/factorial.c +++ b/progs/factorial.c @@ -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);