]> code.bitgloo.com Git - clyne/interpreter.git/commitdiff
fixed string memory leaks
authorClyne Sullivan <tullivan99@gmail.com>
Mon, 2 Apr 2018 20:34:22 +0000 (16:34 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Mon, 2 Apr 2018 20:34:22 +0000 (16:34 -0400)
ops.c
parser.c
test6 [new file with mode: 0644]

diff --git a/ops.c b/ops.c
index 74ec73b02d74e9b6313b5436827831370c760b17..8e6cc66963b8e03ad09225c89e9c91f1bcd24325 100644 (file)
--- a/ops.c
+++ b/ops.c
@@ -239,8 +239,7 @@ OP_DEF(set)
                r->value.f = a->value.f;
        } else if (b->type == STRING) {
                a->type = STRING;
-               if (a->value.p != 0)
-                       free((void *)a->value.p);
+               free((void *)a->value.p);
                a->value.p = (uint32_t)strclone((char *)b->value.p);
                r->type = STRING;
                r->value.p = (uint32_t)strclone((char *)a->value.p);
index cc09d992cbe0d39b10f9abaef4642968b43f0db4..68104f65c8d917fd6345cd1d835ed3d3bbc803f9 100644 (file)
--- a/parser.c
+++ b/parser.c
@@ -75,6 +75,8 @@ instance *inewinstance(void)
 void idelline(variable **ops);
 void idelinstance(instance *it)
 {
+       itryfree(it->ret);
+
        for (uint32_t i = 0; i < MAX_LINES; i++) {
                if (it->lines[i] == 0)
                        continue;
@@ -93,7 +95,6 @@ void idelinstance(instance *it)
        free(it->names);
 
        free(it->stack);
-       itryfree(it->ret);
        free(it);
 }
 
@@ -230,7 +231,12 @@ loop:
                // move result global variable "ANS"
                variable *ret = igetvar(it, "ANS");
                ret->type = it->ret->type;
-               ret->value.p = it->ret->value.p;
+               if (ret->type == STRING) {
+                       free((void *)ret->value.p);
+                       ret->value.p = (uint32_t)strclone((char *)it->ret->value.p);
+               } else {
+                       ret->value.p = it->ret->value.p;
+               }
                itryfree(it->ret);
                it->ret = ret;
        }
@@ -349,7 +355,7 @@ variable *isolve_(instance *it, variable **ops, uint32_t count)
                                        return 0;
 
                                if (!(it->sindent & SKIP)) {
-                                       variable *v = !ops[aidx]->tmp ? varclone(ops[aidx]) : ops[aidx];
+                                       variable *v = !ops[aidx]->tmp ? make_varf(0, 0.0f) : ops[aidx];
                                        if (func(v, ops[aidx], ops[bidx]) != 0)
                                                return 0;
                                        ops[aidx] = v;
diff --git a/test6 b/test6
new file mode 100644 (file)
index 0000000..bc17de2
--- /dev/null
+++ b/test6
@@ -0,0 +1,10 @@
+# test6
+# used to fix memory leak in setting defined strings
+
+test = "hello"
+test = "fail"
+a = test
+test = "acd"
+
+print(a)
+print(test)