From 609ff568843dae9d003153509aaa0080aea7a303 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Thu, 22 Feb 2018 11:14:53 -0500 Subject: [PATCH] memory leak fixes, stuff --- builtins.c | 11 ++++++++--- builtins.h | 2 +- parser.c | 36 +++++++++++++++++++----------------- variable.c | 7 +++---- variable.h | 4 ++-- 5 files changed, 33 insertions(+), 27 deletions(-) diff --git a/builtins.c b/builtins.c index 44c058e..9c82b8d 100644 --- a/builtins.c +++ b/builtins.c @@ -105,9 +105,8 @@ int ifunc_while(interpreter *it) return 0; } -int ifunc_ret(interpreter *it) +void iret(interpreter *it, variable *v) { - variable *v = igetarg(it, 0); switch (v->valtype) { case INTEGER: inew_integer(it, "RET", INT(v)); @@ -119,7 +118,7 @@ int ifunc_ret(interpreter *it) inew_string(it, "RET", v->svalue); break; default: - return -1; + return; break; } if (it->ret != 0) { @@ -128,6 +127,12 @@ int ifunc_ret(interpreter *it) it->ret->svalue = v->svalue; it->ret = 0; } +} + +int ifunc_ret(interpreter *it) +{ + variable *v = igetarg(it, 0); + iret(it, v); return 0; } diff --git a/builtins.h b/builtins.h index 0c96452..9d18093 100644 --- a/builtins.h +++ b/builtins.h @@ -8,7 +8,7 @@ void iload_core(interpreter *it); -int ifunc_ret(interpreter *it); +void iret(interpreter *it, variable *v); const func_t indent_up[IUP_COUNT]; const func_t indent_down[IDOWN_COUNT]; diff --git a/parser.c b/parser.c index ed7b16b..b2d27e6 100644 --- a/parser.c +++ b/parser.c @@ -22,21 +22,22 @@ char *str_undef = "(undefined)"; void iinit(interpreter *interp) { - interp->vars = (variable *)malloc(MAX_VARS * sizeof(variable)); - interp->vnames = (char **)malloc(MAX_VARS * sizeof(char *)); - interp->stack = (stack_t *)malloc(MAX_STACK * sizeof(stack_t)); + interp->vars = (variable *)calloc(MAX_VARS, sizeof(variable)); + interp->vnames = (char **)calloc(MAX_VARS, sizeof(char *)); + interp->stack = (stack_t *)calloc(MAX_STACK, sizeof(stack_t)); interp->stidx = 0; - interp->lines = (variable ***)malloc(MAX_LINES * sizeof(variable **)); + interp->lines = (variable ***)calloc(MAX_LINES, sizeof(variable **)); interp->lnidx = 0; interp->indent = 0; interp->sindent = 0; + interp->ret = 0; - for (unsigned int i = 0; i < MAX_VARS; i++) { - interp->vars[i].used = 0; - interp->vnames[i] = 0; - } - for (unsigned int i = 0; i < MAX_LINES; i++) - interp->lines[i] = 0; + //for (unsigned int i = 0; i < MAX_VARS; i++) { + // interp->vars[i].used = 0; + // interp->vnames[i] = 0; + //} + //for (unsigned int i = 0; i < MAX_LINES; i++) + // interp->lines[i] = 0; iload_core(interp); } @@ -75,7 +76,8 @@ variable *interpreter_get_variable(interpreter *interp, const char *name) v->valtype = STRING; v->value = 0; v->svalue = str_undef; - interp->vnames[i] = strclone(name); + char *s = strclone(name); + interp->vnames[i] = s; return v; } else if (interp->vnames[i] != 0 && !strcmp(interp->vnames[i], name)) { return &interp->vars[i]; @@ -187,9 +189,9 @@ variable *make_var(interpreter *interp, const char *line, uint32_t *next) } } variable *v; - if (dec) { + if (dec) v = vmakef(strtof(line, 0)); - } else + else v = vmake(0, INTEGER, (void *)atoi(line)); *next = end; return v; @@ -208,7 +210,8 @@ int idoline(interpreter *interp, const char *line) if (line[offset] == '#') return 0; - interp->lines[interp->lnidx] = (variable **)calloc(8, sizeof(variable *)); + variable **linebuf = (variable **)calloc(8, sizeof(variable *)); + interp->lines[interp->lnidx] = linebuf; variable **ops = interp->lines[interp->lnidx]; // step 1 - convert to tokens @@ -355,7 +358,7 @@ fail: variable *idoexpr(interpreter *interp, const char *line) { - variable *result = (variable *)malloc(sizeof(variable)); + variable *result = (variable *)calloc(1, sizeof(variable)); char *mline = line; void *ops[16]; uint32_t ooffset = 0; @@ -430,9 +433,8 @@ variable *idoexpr(interpreter *interp, const char *line) // step 2 - do operations result->valtype = ((variable *)ops[0])->valtype; result->value = ((variable *)ops[0])->value; - for (uint32_t i = 1; i < ooffset; i += 2) { + for (uint32_t i = 1; i < ooffset; i += 2) iopfuncs[(uint32_t)ops[i] - 1](result, result, ops[i + 1]); - } for (uint32_t i = 0; i < ooffset; i += 2) { if (!((variable *)ops[i])->used) { diff --git a/variable.c b/variable.c index 38b771b..bcf96cb 100644 --- a/variable.c +++ b/variable.c @@ -12,10 +12,9 @@ extern char *str_func; char *fixstring(char *s) { - int len = strlen(s); - char *n = malloc(len + 1); - int i, j; - for (i = 0, j = 0; s[i] != '\0'; i++, j++) { + char *n = malloc(strlen(s) + 1); + int j = 0; + for (int i = 0; s[i] != '\0'; i++, j++) { if (s[i] == '\\') { if (s[i + 1] == 'n') n[j] = '\n'; diff --git a/variable.h b/variable.h index e5aa20c..beb8be3 100644 --- a/variable.h +++ b/variable.h @@ -3,8 +3,8 @@ #include -#define INT(v) (*((int32_t *)&v->value)) -#define FLOAT(v) (*((float *)&v->value)) +#define INT(v) (*((int32_t *)&(v)->value)) +#define FLOAT(v) (*((float *)&(v)->value)) typedef struct { uint8_t used :1;