From 5c6069dbe9f291b1a135f90c4add4be96af2e2b9 Mon Sep 17 00:00:00 2001 From: ISSOtm Date: Wed, 13 May 2020 01:17:58 +0200 Subject: [PATCH] Add NULL and overflow checks to macro args --- src/asm/macro.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/src/asm/macro.c b/src/asm/macro.c index dcc27422..8ec76100 100644 --- a/src/asm/macro.c +++ b/src/asm/macro.c @@ -1,5 +1,6 @@ #include +#include #include #include #include @@ -46,22 +47,32 @@ struct MacroArgs *macro_NewArgs(void) { struct MacroArgs *args = malloc(SIZEOF_ARGS(INITIAL_ARG_SIZE)); + if (!args) + fatalerror("Unable to register macro arguments: %s", strerror(errno)); + args->nbArgs = 0; args->shift = 0; args->capacity = INITIAL_ARG_SIZE; return args; } -void macro_AppendArg(struct MacroArgs **args, char *s) +void macro_AppendArg(struct MacroArgs **argPtr, char *s) { - if ((**args).nbArgs == MAXMACROARGS) +#define macArgs (*argPtr) + if (macArgs->nbArgs == MAXMACROARGS) yyerror("A maximum of " EXPAND_AND_STR(MAXMACROARGS) " arguments is allowed"); - if ((**args).nbArgs == (**args).capacity) { - (**args).capacity *= 2; - *args = realloc(*args, SIZEOF_ARGS((**args).capacity)); + if (macArgs->nbArgs >= macArgs->capacity) { + macArgs->capacity *= 2; + /* Check that overflow didn't roll us back */ + if (macArgs->capacity <= macArgs->nbArgs) + fatalerror("Failed to add new macro argument: possible capacity overflow"); + macArgs = realloc(macArgs, SIZEOF_ARGS(macArgs->capacity)); + if (!macArgs) + fatalerror("Error adding new macro argument: %s", strerror(errno)); } - (**args).args[(**args).nbArgs++] = s; + macArgs->args[macArgs->nbArgs++] = s; +#undef macArgs } void macro_UseNewArgs(struct MacroArgs *args)