From 97c9dba402d6d4097cbdc4c44c00222c53122033 Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Sun, 13 Feb 2022 12:12:42 +0100 Subject: Fixed bug in reading scrambles with unmatched parentheses --- src/alg.c | 13 +++++++++++-- src/commands.c | 13 +------------ 2 files changed, 12 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/alg.c b/src/alg.c index 27af676..4f5079a 100644 --- a/src/alg.c +++ b/src/alg.c @@ -268,27 +268,31 @@ move_string(Move m) Alg * new_alg(char *str) { - Alg *alg = malloc(sizeof(Alg)); + Alg *alg; int i; - bool niss = false, move_read; + bool niss, move_read; Move j, m; + alg = malloc(sizeof(Alg)); alg->move = malloc(30 * sizeof(Move)); alg->inv = malloc(30 * sizeof(bool)); alg->allocated = 30; alg->len = 0; + niss = false; for (i = 0; str[i]; i++) { if (str[i] == ' ' || str[i] == '\t' || str[i] == '\n') continue; if (str[i] == '(' && niss) { fprintf(stderr, "Error reading moves: nested ( )\n"); + alg->len = 0; return alg; } if (str[i] == ')' && !niss) { fprintf(stderr, "Error reading moves: unmatched )\n"); + alg->len = 0; return alg; } @@ -353,6 +357,11 @@ new_alg(char *str) } } + if (niss) { + fprintf(stderr, "Error reading moves: unmatched (\n"); + alg->len = 0; + } + return alg; } diff --git a/src/commands.c b/src/commands.c index 740736c..d9d6b50 100644 --- a/src/commands.c +++ b/src/commands.c @@ -519,25 +519,14 @@ read_scramble(int c, char **v, CommandArgs *args) int i, k, n; unsigned int j; char *algstr; - Alg *aux; if (c < 1) { fprintf(stderr, "Error: no scramble given?\n"); return false; } - n = 0; - for(i = 0; i < c; i++) { - aux = new_alg(v[i]); - if (aux->len == 0) { - fprintf(stderr, "Error: %s or its argument" - "unrecognized\n", v[i]); - free(aux); - return false; - } - free(aux); + for(n = 0, i = 0; i < c; i++) n += strlen(v[i]); - } algstr = malloc((n + 1) * sizeof(char)); k = 0; -- cgit v1.3