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

@ -229,7 +229,6 @@ static void DohString_append(DOH *so, const DOHString_or_char *str) {
if (newlen >= newmaxsize - 1)
newmaxsize = newlen + 1;
s->str = (char *) DohRealloc(s->str, newmaxsize);
assert(s->str);
s->maxsize = newmaxsize;
}
tc = s->str;
@ -296,7 +295,6 @@ static int String_insert(DOH *so, int pos, DOH *str) {
while (s->maxsize <= s->len + len) {
int newsize = 2 * s->maxsize;
s->str = (char *) DohRealloc(s->str, newsize);
assert(s->str);
s->maxsize = newsize;
}
memmove(s->str + pos + len, s->str + pos, (s->len - pos));
@ -424,7 +422,6 @@ static int String_write(DOH *so, const void *buffer, int len) {
newlen = s->sp + len + 1;
if (newlen > s->maxsize) {
s->str = (char *) DohRealloc(s->str, newlen);
assert(s->str);
s->maxsize = newlen;
s->len = s->sp + len;
}
@ -517,7 +514,6 @@ static int String_putc(DOH *so, int ch) {
if (len > (maxsize - 2)) {
maxsize *= 2;
tc = (char *) DohRealloc(tc, maxsize);
assert(tc);
s->maxsize = (int) maxsize;
s->str = tc;
}
@ -923,7 +919,6 @@ static int replace_simple(String *str, char *token, char *rep, int flags, int co
newsize *= 2;
ns = (char *) DohMalloc(newsize);
assert(ns);
t = ns;
s = first;