From d32ea0b043296f097a9a4788407f080503ea998f Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Thu, 26 Jan 2023 11:55:51 +0100 Subject: [PATCH] exit directly when memory allocation failed --- calc.c | 4 ++-- parser.c | 50 +++++--------------------------------------------- 2 files changed, 7 insertions(+), 47 deletions(-) diff --git a/calc.c b/calc.c index 5e6a78f..ed67da4 100644 --- a/calc.c +++ b/calc.c @@ -161,7 +161,7 @@ int main (int argc, char *argv[]) if (element == ERROR_OP) { VERBOSE (WARNING, fprintf (stdout, "error while parsing: '%s'\n", line[i]); fflush (stdout)); ret = 1; - } else { + } else if (element != NULL) { VERBOSE (INFO, print_element (element, 0)); answer = evaluate_element (element, 0); fprintf (stdout, format, answer); @@ -297,6 +297,6 @@ int main (int argc, char *argv[]) // test: echo -e '{sto (1, 1 + 1), rcl (1) * 3}' | calc.exe -v 3 | grep -q 'Program' // test: echo -e '{sto (1, 1 + 1), rcl (1) * 3}' | calc.exe | grep -q '=> 6' // test: echo -e '{\n{}\n{1, 2\n{sto (1, 1 + 1),\npow(2, {sto (1, 1 + 2), 2}, {rcl(2)})\n2 {sto (1, 1 + 1)}' | calc.exe | grep -c error | xargs test 6 = -// test: echo -e '1 }\n1 )\n1 , 2' | calc.exe | grep -c error | xargs test 3 = +// test: echo -e '1 }\n1 )\n1 , 2\n ' | calc.exe | grep -c error | xargs test 3 = /* vim: set ts=4 sw=4 et: */ diff --git a/parser.c b/parser.c index e6fc31e..9796048 100644 --- a/parser.c +++ b/parser.c @@ -43,14 +43,13 @@ element_t *newelement (func_t function, int nbops, int prio) element_t *new = (element_t *) calloc (1, sizeof (element_t)); if (new == NULL) { VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n")); - return NULL; + exit (1); } if (nbops) { new->ops = (element_t **) calloc (nbops, sizeof (element_t *)); if (new->ops == NULL) { - free (new); VERBOSE (ERROR, fprintf (stderr, "can't allocate memory\n")); - return NULL; + exit (1); } } new->func = function; @@ -89,16 +88,9 @@ element_t *dupelement (element_t *root) return root; } tmp = newelement (root->func, root->nbops, root->prio); - if (tmp == NULL) { - return ERROR_OP; - } tmp->value = root->value; for (i = 0; i < root->nbops; i++) { tmp->ops[i] = dupelement (root->ops[i]); - if (tmp->ops[i] == ERROR_OP) { - delelement (tmp); - return ERROR_OP; - } } return tmp; } @@ -156,9 +148,6 @@ keyword_t constants[NB_CONSTANTS] = { element_t *subparser (element_t **proot, char **pstr, func_t func, int nbops, int prio) { element_t *new = newelement (func, nbops, prio); - if (new == NULL) { - return ERROR_OP; - } new->ops[0] = *proot; new->ops[1] = parser (*pstr, pstr, new->prio); if ((new->ops[1] == NULL) || ((new->ops[1] != ERROR_OP) && (new->ops[1]->prio == 9))) { @@ -171,10 +160,6 @@ element_t *subparser (element_t **proot, char **pstr, func_t func, int nbops, in return ERROR_OP; } *proot = newelement (Val, 1, 5); - if (*proot == NULL) { - delelement (new); - return ERROR_OP; - } (*proot)->ops[0] = new; return *proot; @@ -212,10 +197,6 @@ element_t *parser (char *str, char **next, int prio) } element_t **prog = NULL; new = newelement (Prog, 0, 5); - if (new == NULL) { - delelement (root); - return ERROR_OP; - } root = new; prog = &root; @@ -279,9 +260,6 @@ element_t *parser (char *str, char **next, int prio) } while (*str == ','); } else { root = newelement (Val, 1, 5); - if (root == NULL) { - return ERROR_OP; - } new = parser (str + 1, &str, 0); if ((new == NULL) || ((new != ERROR_OP) && (new->prio == 9))) { delelement (new); @@ -335,11 +313,7 @@ element_t *parser (char *str, char **next, int prio) return ERROR_OP; } } else if (*str == '-') { - new = newelement (Sig, 1, 9); - if (new == NULL) { - return ERROR_OP; - } - root = new; + root = newelement (Sig, 1, 9); } else { return ERROR_OP; } @@ -360,11 +334,7 @@ element_t *parser (char *str, char **next, int prio) VERBOSE (DEBUG, fprintf (stdout, "start processing function\n")); if (root == NULL) { VERBOSE (INFO, fprintf (stdout, "Func: %d\n", function->func)); - new = newelement (function->func, function->nbops, function->prio); - if (new == NULL) { - return ERROR_OP; - } - root = new; + root = newelement (function->func, function->nbops, function->prio); } else { delelement (root); return ERROR_OP; @@ -387,11 +357,7 @@ element_t *parser (char *str, char **next, int prio) VERBOSE (DEBUG, fprintf (stdout, "start processing constant\n")); if (root == NULL) { VERBOSE (INFO, fprintf (stdout, "Const: %d\n", constant->func)); - new = newelement (constant->func, constant->nbops, constant->prio); - if (new == NULL) { - return ERROR_OP; - } - root = new; + root = newelement (constant->func, constant->nbops, constant->prio); } else { delelement (root); return ERROR_OP; @@ -417,9 +383,6 @@ element_t *parser (char *str, char **next, int prio) if (str != pt) { if ((root == NULL) || (root->prio == 6)) { new = newelement (Val, 1, 5); - if (new == NULL) { - return ERROR_OP; - } new->value = value; if (root == NULL) { root = new; @@ -601,9 +564,6 @@ double while_do (element_t *cond, element_t *action) element_t *temp = NULL; VERBOSE (DEBUG, fprintf (stdout, "starting while loop\n")); - if (cond == NULL) { - return ret; - } while (1) { VERBOSE (DEBUG, fprintf (stdout, "loop...\n")); -- 2.30.2