From 83a8355c8942555e2fe0e3fc1d480a52f91ec475 Mon Sep 17 00:00:00 2001 From: Sven Verdoolaege Date: Wed, 27 Aug 2008 11:22:35 +0200 Subject: [PATCH] barvinok_enumerate_e: avoid infinite loop when presented with incorrect input --- barvinok_enumerate_e.cc | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/barvinok_enumerate_e.cc b/barvinok_enumerate_e.cc index 8b18ef7..643207a 100644 --- a/barvinok_enumerate_e.cc +++ b/barvinok_enumerate_e.cc @@ -1,3 +1,4 @@ +#include #include #include #include @@ -123,6 +124,20 @@ static void verify_results(Polyhedron *P, evalue *EP, gen_fun *gf, int exist, int nparam, arguments *options); +static char *next_line(FILE *input, char *line, unsigned len) +{ + char *p; + + do { + if (!(p = fgets(line, len, input))) + return NULL; + while (isspace(*p) && *p != '\n') + ++p; + } while (*p == '#' || *p == '\n'); + + return p; +} + int main(int argc, char **argv) { Polyhedron *A; @@ -158,13 +173,17 @@ int main(int argc, char **argv) A = Constraints2Polyhedron(MA, options->MaxRays); Matrix_Free(MA); - fgets(s, 128, stdin); - while ((*s=='#') || (sscanf(s, "E %d", &exist)<1)) - fgets(s, 128, stdin); + exist = -1; + while (next_line(stdin, s, sizeof(s))) + if (sscanf(s, "E %d", &exist) == 1) + break; + assert(exist >= 0); - fgets(s, 128, stdin); - while ((*s=='#') || (sscanf(s, "P %d", &nparam)<1)) - fgets(s, 128, stdin); + nparam = -1; + while (next_line(stdin, s, sizeof(s))) + if (sscanf(s, "P %d", &nparam) == 1) + break; + assert(nparam >= 0); /******* Read the options: initialize Min and Max ********/ -- 2.11.4.GIT