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:
parent
9eb75a0c07
commit
e38847f7e1
14 changed files with 83 additions and 65 deletions
|
|
@ -209,14 +209,14 @@ void Swig_warnfilter(const_String_or_char_ptr wlist, int add) {
|
|||
Insert(filter, 0, "-");
|
||||
}
|
||||
} else {
|
||||
char *temp = (char *)malloc(sizeof(char)*strlen(c) + 2);
|
||||
char *temp = (char *)Malloc(sizeof(char)*strlen(c) + 2);
|
||||
if (isdigit((int) *c)) {
|
||||
sprintf(temp, "-%s", c);
|
||||
} else {
|
||||
strcpy(temp, c);
|
||||
}
|
||||
Replace(filter, temp, "", DOH_REPLACE_FIRST);
|
||||
free(temp);
|
||||
Free(temp);
|
||||
}
|
||||
}
|
||||
c = strtok(NULL, ", ");
|
||||
|
|
|
|||
|
|
@ -32,16 +32,12 @@ static int *marked;
|
|||
* ----------------------------------------------------------------------------- */
|
||||
|
||||
void Swig_init_args(int argc, char **argv) {
|
||||
int i;
|
||||
assert(argc > 0);
|
||||
assert(argv);
|
||||
|
||||
numargs = argc;
|
||||
args = argv;
|
||||
marked = (int *) malloc(numargs * sizeof(int));
|
||||
for (i = 0; i < argc; i++) {
|
||||
marked[i] = 0;
|
||||
}
|
||||
marked = (int *) Calloc(numargs, sizeof(int));
|
||||
marked[0] = 1;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ static char *fake_version = 0;
|
|||
char *Swig_copy_string(const char *s) {
|
||||
char *c = 0;
|
||||
if (s) {
|
||||
c = (char *) malloc(strlen(s) + 1);
|
||||
c = (char *) Malloc(strlen(s) + 1);
|
||||
strcpy(c, s);
|
||||
}
|
||||
return c;
|
||||
|
|
@ -1251,7 +1251,7 @@ void Swig_offset_string(String *s, int number) {
|
|||
if ((Char(s))[len-1] == '\n')
|
||||
--lines;
|
||||
/* allocate a temporary storage for a padded string */
|
||||
res = (char*)malloc(len + lines * number * 2 + 1);
|
||||
res = (char*)Malloc(len + lines * number * 2 + 1);
|
||||
res[len + lines * number * 2] = 0;
|
||||
|
||||
/* copy lines to res, prepending tabs to each line */
|
||||
|
|
@ -1275,7 +1275,7 @@ void Swig_offset_string(String *s, int number) {
|
|||
/* replace 's' contents with 'res' */
|
||||
Clear(s);
|
||||
Append(s, res);
|
||||
free(res);
|
||||
Free(res);
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -1458,11 +1458,11 @@ String *Swig_string_regex(String *s) {
|
|||
|
||||
String *Swig_pcre_version(void) {
|
||||
int len = pcre2_config(PCRE2_CONFIG_VERSION, NULL);
|
||||
char *buf = malloc(len);
|
||||
char *buf = Malloc(len);
|
||||
String *result;
|
||||
pcre2_config(PCRE2_CONFIG_VERSION, buf);
|
||||
result = NewStringf("PCRE2 Version: %s", buf);
|
||||
free(buf);
|
||||
Free(buf);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -56,7 +56,7 @@ static void brackets_clear(Scanner *);
|
|||
|
||||
Scanner *NewScanner(void) {
|
||||
Scanner *s;
|
||||
s = (Scanner *) malloc(sizeof(Scanner));
|
||||
s = (Scanner *) Malloc(sizeof(Scanner));
|
||||
s->line = 1;
|
||||
s->file = 0;
|
||||
s->nexttoken = -1;
|
||||
|
|
@ -88,8 +88,8 @@ void DelScanner(Scanner *s) {
|
|||
Delete(s->file);
|
||||
Delete(s->error);
|
||||
Delete(s->str);
|
||||
free(s->idstart);
|
||||
free(s);
|
||||
Free(s->idstart);
|
||||
Free(s);
|
||||
}
|
||||
|
||||
/* -----------------------------------------------------------------------------
|
||||
|
|
@ -202,7 +202,7 @@ int Scanner_start_line(Scanner *s) {
|
|||
* ----------------------------------------------------------------------------- */
|
||||
|
||||
void Scanner_idstart(Scanner *s, const char *id) {
|
||||
free(s->idstart);
|
||||
Free(s->idstart);
|
||||
s->idstart = Swig_copy_string(id);
|
||||
}
|
||||
|
||||
|
|
@ -336,9 +336,9 @@ static void brackets_reset(Scanner *s) {
|
|||
* Usually called when '(' is found.
|
||||
* ----------------------------------------------------------------------------- */
|
||||
static void brackets_push(Scanner *s) {
|
||||
int *newInt = (int *)malloc(sizeof(int));
|
||||
int *newInt = (int *)Malloc(sizeof(int));
|
||||
*newInt = 0;
|
||||
Push(s->brackets, NewVoid(newInt, free));
|
||||
Push(s->brackets, NewVoid(newInt, Free));
|
||||
}
|
||||
|
||||
/* -----------------------------------------------------------------------------
|
||||
|
|
@ -1811,14 +1811,14 @@ void Scanner_locator(Scanner *s, String *loc) {
|
|||
cparse_file = locs->filename;
|
||||
cparse_line = locs->line_number;
|
||||
l = locs->next;
|
||||
free(locs);
|
||||
Free(locs);
|
||||
locs = l;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/* We're going to push a new location */
|
||||
l = (Locator *) malloc(sizeof(Locator));
|
||||
l = (Locator *) Malloc(sizeof(Locator));
|
||||
l->filename = cparse_file;
|
||||
l->line_number = cparse_line;
|
||||
l->next = locs;
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ static int Max_line_size = 128;
|
|||
|
||||
Wrapper *NewWrapper(void) {
|
||||
Wrapper *w;
|
||||
w = (Wrapper *) malloc(sizeof(Wrapper));
|
||||
w = (Wrapper *) Malloc(sizeof(Wrapper));
|
||||
w->localh = NewHash();
|
||||
w->locals = NewStringEmpty();
|
||||
w->code = NewStringEmpty();
|
||||
|
|
@ -46,7 +46,7 @@ void DelWrapper(Wrapper *w) {
|
|||
Delete(w->locals);
|
||||
Delete(w->code);
|
||||
Delete(w->def);
|
||||
free(w);
|
||||
Free(w);
|
||||
}
|
||||
|
||||
/* -----------------------------------------------------------------------------
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue