ref: 5c6069dbe9f291b1a135f90c4add4be96af2e2b9
parent: d517d2d6b48ef4a10c2ac1c86b54b62275ff3ec8
author: ISSOtm <[email protected]>
date: Tue May 12 21:17:58 EDT 2020
Add NULL and overflow checks to macro args
--- a/src/asm/macro.c
+++ b/src/asm/macro.c
@@ -1,5 +1,6 @@
#include <assert.h>
+#include <errno.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
@@ -46,6 +47,9 @@
{
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;
@@ -52,16 +56,23 @@
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)