Fail cleanly on allocation failures

Previously code in the SWIG tool didn't handle allocation failures
well.  Most places didn't check for NULL return from
malloc()/realloc()/calloc() at all, typically resulting in undefined
behaviour, and some places used assert() to check for a NULL return
(which is a misuse of assert() and such checks disappear if built with
NDEBUG defined leaving us back with undefined behaviour).

All C allocations are now done via wrapper functions (Malloc(),
Realloc() and Calloc()) which emit and error and exit with non-zero
status on failure, so a non-NULL return can be relied upon.

Fixes #1901.
This commit is contained in:
Olly Betts 2022-03-03 17:45:03 +13:00 committed by Olly Betts
commit e38847f7e1
14 changed files with 83 additions and 65 deletions

View file

@ -32,7 +32,7 @@
/* We do this for portability */
#undef alloca
#define alloca malloc
#define alloca Malloc
/* -----------------------------------------------------------------------------
* Externals

View file

@ -620,7 +620,7 @@ static Node *template_locate(String *name, Parm *tparms, Symtab *tscope) {
int parms_len = ParmList_len(parms);
int *priorities_row;
max_possible_partials = Len(partials);
priorities_matrix = (int *)malloc(sizeof(int) * max_possible_partials * parms_len); /* slightly wasteful allocation for max possible matches */
priorities_matrix = (int *)Malloc(sizeof(int) * max_possible_partials * parms_len); /* slightly wasteful allocation for max possible matches */
priorities_row = priorities_matrix;
for (pi = First(partials); pi.item; pi = Next(pi)) {
Parm *p = parms;
@ -818,7 +818,7 @@ success:
Printf(stdout, " chosen template:'%s'\n", Getattr(n, "name"));
}
Delete(parms);
free(priorities_matrix);
Free(priorities_matrix);
return n;
}