Compare commits

...

4 commits

Author SHA1 Message Date
Jeff Epler
7cec815530 Don't allocate args when instruction parsing fails
.. it looks like (opcode == -1) should maybe lead to an error return
rather than a continue, but in any case we need to avoid leaking
'args' in this situation.

.. the previous arrangement lead to compiler diagnostics when
building with 'scan-build make', such as:

libtvm/tvm_parser.c:186:39: warning: Potential leak of memory pointed to by 'args'
        for (line_idx = 0; tokens[line_idx]; line_idx++) {
                                             ^~~~~~~~
1 warning generated.
2018-03-17 14:02:50 -05:00
Jeff Epler
523a88ad3a Free memory in exit paths
.. the previous arrangement lead to compiler diagnostics when
building with 'scan-build make', such as:

libtvm/tvm_parser.c:218:12: warning: Potential leak of memory pointed to by 'args'
                        return -1;
                                ^
2018-03-17 14:00:36 -05:00
Jeff Epler
110a02f9b5 Delete dead code
.. the previous arrangement lead to compiler diagnostics when
building with 'scan-build make':

libtvm/tvm_htab.c:113:3: warning: Value stored to 'node' is never read
                node = node->next;
                ^      ~~~~~~~~~~
2018-03-17 13:48:25 -05:00
Jeff Epler
75f2a1e43e Move definition of tvm_opcode_map, tvm_register_map out of header
.. the previous arrangement lead to compiler diagnostics when
building with 'scan-build make':

include/tvm/tvm_tokens.h:17:20: error: ‘tvm_register_map’ defined but not used [-Werror=unused-variable]
 static const char *tvm_register_map[] = {
                    ^~~~~~~~~~~~~~~~
include/tvm/tvm_tokens.h:7:20: error: ‘tvm_opcode_map’ defined but not used [-Werror=unused-variable]
 static const char *tvm_opcode_map[] = {
                    ^~~~~~~~~~~~~~
cc1: all warnings being treated as errors
2018-03-17 13:46:24 -05:00
4 changed files with 44 additions and 19 deletions

View file

@ -4,21 +4,8 @@
#define TOK_INCLUDE "%include"
#define TOK_DEFINE "%define"
static const char *tvm_opcode_map[] = {
"nop", "int", "mov",
"push", "pop", "pushf", "popf",
"inc", "dec", "add", "sub", "mul", "div", "mod", "rem",
"not", "xor", "or", "and", "shl", "shr",
"cmp", "jmp", "call", "ret",
"je", "jne", "jg", "jge", "jl", "jle",
"prn", 0
};
extern const char *tvm_opcode_map[];
static const char *tvm_register_map[] = {
"eax", "ebx", "ecx", "edx",
"esi", "edi", "esp", "ebp",
"eip",
"r08", "r09", "r10", "r11",
"r12", "r13", "r14", "r15", 0};
extern const char *tvm_register_map[];
#endif

View file

@ -110,7 +110,6 @@ static struct tvm_htab_node *htab_add_core(
node = node->next;
prev = node;
node = node->next;
}
/* Allocate space, and copy the key/value pair. */

View file

@ -3,6 +3,24 @@
#include <tvm/tvm_lexer.h>
#include <tvm/tvm_tokens.h>
const char *tvm_opcode_map[] = {
"nop", "int", "mov",
"push", "pop", "pushf", "popf",
"inc", "dec", "add", "sub", "mul", "div", "mod", "rem",
"not", "xor", "or", "and", "shl", "shr",
"cmp", "jmp", "call", "ret",
"je", "jne", "jg", "jge", "jl", "jle",
"prn", 0
};
const char *tvm_register_map[] = {
"eax", "ebx", "ecx", "edx",
"esi", "edi", "esp", "ebp",
"eip",
"r08", "r09", "r10", "r11",
"r12", "r13", "r14", "r15", 0};
static int *token_to_register(const char *token, struct tvm_mem *mem);
static int instr_to_opcode(const char *instr);
@ -119,6 +137,19 @@ static int **tvm_parse_args(
return args;
}
/* This function frees the memory allocated by tvm_parse_args().
*/
static void tvm_free_args(int **args) {
if(args) {
for (int i = 0; args[i]; i++) {
free(args[i]);
}
}
free(args);
}
/* This is a helper function that converts one instruction,
@ -158,10 +189,13 @@ int tvm_parse_program(
int opcode = tvm_parse_instr(
vm, tokens[line_idx], &instr_place);
if (opcode == -1)
continue;
int **args = tvm_parse_args(
vm, tokens[line_idx], &instr_place);
if (opcode == -1 || !args)
if (!args)
continue;
void *newptr;
@ -172,8 +206,10 @@ int tvm_parse_program(
if (newptr != NULL) {
vm->prog->instr = newptr;
vm->prog->instr[vm->prog->num_instr - 1] = opcode;
} else
} else {
tvm_free_args(args);
return -1;
}
newptr = realloc(
vm->prog->args,
@ -181,8 +217,10 @@ int tvm_parse_program(
if (newptr != NULL)
vm->prog->args = (int ***)newptr;
else
else {
tvm_free_args(args);
return -1;
}
vm->prog->args[vm->prog->num_instr - 1] = args;
}

View file

@ -29,6 +29,7 @@ static int process_includes(
if (!filp) {
printf("Unable to open file \"%s\"\n", filename);
free(temp_str);
return -1;
}