Skip to content

Commit

Permalink
Consistent allocateMemory usage, error on allocating memory
Browse files Browse the repository at this point in the history
  • Loading branch information
envenomator committed Dec 14, 2024
1 parent f02467a commit 6922806
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/assemble.c
Original file line number Diff line number Diff line change
Expand Up @@ -1289,6 +1289,7 @@ struct contentitem *insertContent(char *filename) {
if(ci->fh == 0) return NULL;
ci->size = ioGetfilesize(ci->fh);
ci->buffer = allocateMemory(ci->size+1);
if(ci->buffer == NULL) return NULL;
ci->readptr = ci->buffer;
if(fread(ci->buffer, 1, ci->size, ci->fh) != ci->size) {
error(message[ERROR_READINGINPUT],0);
Expand Down
2 changes: 1 addition & 1 deletion src/getopt.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ exchange (char **argv)
{
/* We must extend the array. The user plays games with us and
presents new arguments. */
char *new_str = (char *) malloc (top + 1);
char *new_str = (char *) allocateMemory (top + 1);
if (new_str == NULL)
nonoption_flags_len = nonoption_flags_max_len = 0;
else
Expand Down
8 changes: 4 additions & 4 deletions src/label.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,14 @@ bool insertLabel(char *labelname, uint8_t len, int24_t labelAddress, bool local)
label_t *tmp,*try;

// allocate space in buffer for label_t struct
tmp = (label_t *)malloc(sizeof(label_t));
tmp = (label_t *)allocateMemory(sizeof(label_t));
labelmemsize += sizeof(label_t);
if(tmp == 0) return false;
if(tmp == NULL) return false;

// allocate space in buffer for string and store it to buffer
tmp->name = (char*)malloc(len+1);
tmp->name = (char*)allocateMemory(len+1);
labelmemsize += (uint24_t)len +1;
if(tmp->name == 0) return false;
if(tmp->name == NULL) return false;

strcpy(tmp->name, labelname);
tmp->local = local;
Expand Down
27 changes: 13 additions & 14 deletions src/macro.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@ void initMacros(void) {
}

void setMacroBody(macro_t *macro, const char *body) {
macro->body = (char*)malloc(strlen(body)+1);
if(macro->body == NULL) {
error(message[ERROR_MACROMEMORYALLOCATION],0);
return;
}
macro->body = (char*)allocateMemory(strlen(body)+1);
if(macro->body == NULL) return;
strcpy(macro->body, body);
}

Expand All @@ -34,22 +31,22 @@ macro_t *defineMacro(char *name, uint8_t argcount, char *arguments, uint16_t sta
instruction_t *try, *macroinstruction;

// allocate space in buffer for macro_t struct
tmp = (macro_t *)malloc(sizeof(macro_t));
tmp = (macro_t *)allocateMemory(sizeof(macro_t));
macromemsize += sizeof(macro_t);
if(tmp == 0) return NULL;
if(tmp == NULL) return NULL;

macroinstruction = (instruction_t *)malloc(sizeof(instruction_t));
macroinstruction = (instruction_t *)allocateMemory(sizeof(instruction_t));
macromemsize += sizeof(instruction_t);
if(macroinstruction == 0) return NULL;
if(macroinstruction == NULL) return NULL;

// Link together
macroinstruction->type = MACRO;
macroinstruction->macro = tmp;

len = (unsigned int)strlen(name)+1;
macroinstruction->name = (char *)malloc(len);
macroinstruction->name = (char *)allocateMemory(len);
macromemsize += len;
if(macroinstruction->name == 0) return NULL;
if(macroinstruction->name == NULL) return NULL;

tmp->name = macroinstruction->name;
strcpy(macroinstruction->name, name);
Expand All @@ -60,10 +57,12 @@ macro_t *defineMacro(char *name, uint8_t argcount, char *arguments, uint16_t sta
tmp->substitutions = NULL;
if(argcount == 0) tmp->arguments = NULL;
else {
tmp->arguments = (char **)malloc(argcount * sizeof(char *)); // allocate array of char*
tmp->arguments = (char **)allocateMemory(argcount * sizeof(char *)); // allocate array of char*
macromemsize += argcount * sizeof(char *);
tmp->substitutions = (char **)malloc(argcount * sizeof(char *));
if(tmp->arguments == NULL) return NULL;
tmp->substitutions = (char **)allocateMemory(argcount * sizeof(char *));
macromemsize += argcount * sizeof(char *);
if(tmp->substitutions == NULL) return NULL;
if((tmp->arguments == NULL) || (tmp->substitutions == NULL)) return NULL;

char *argptr = arguments;
Expand All @@ -73,7 +72,7 @@ macro_t *defineMacro(char *name, uint8_t argcount, char *arguments, uint16_t sta
error(message[ERROR_MACROARGLENGTH],0);
return NULL;
}
ptr = (char*)malloc(len+1);
ptr = (char*)allocateMemory(len+1);
macromemsize += len+1;
if(ptr == NULL) return NULL;

Expand Down

0 comments on commit 6922806

Please sign in to comment.