]> git.unchartedbackwaters.co.uk Git - francis/winuae.git/commitdiff
Fix double free and memory leaks
authorToni Wilen <twilen@winuae.net>
Sat, 13 Jan 2024 10:31:10 +0000 (12:31 +0200)
committerToni Wilen <twilen@winuae.net>
Sat, 13 Jan 2024 10:31:10 +0000 (12:31 +0200)
calc.cpp

index c5a929e24a4910eee99724f716fa20dfc683f4d6..98a66af0966b96ab90b28193e05df6e481452a38 100644 (file)
--- a/calc.cpp
+++ b/calc.cpp
@@ -282,6 +282,7 @@ static TCHAR *stacktostring(struct calcstack *st)
         if (_tcslen(st->s) == 1 && st->s[0] >= 'a' && st->s[0] <= 'z') {
             TCHAR *s = parsedvaluess[st->s[0] - 'a'];
             if (s) {
+                xfree(st->vals);
                 st->vals = my_strdup(s);
                 xfree(st->s);
                 st->s = NULL;
@@ -290,6 +291,7 @@ static TCHAR *stacktostring(struct calcstack *st)
             double v = parsedvaluesd[st->s[0] - 'a'];
             TCHAR tmp[256];
             _stprintf(tmp, _T("%d"), (int)v);
+            xfree(st->vals);
             st->vals = my_strdup(tmp);
             xfree(st->s);
             st->s = NULL;
@@ -476,7 +478,7 @@ static TCHAR *chartostack(TCHAR c)
        return s;
 }
 
-static struct calcstack stack[STACK_SIZE] = { 0 };
+static struct calcstack stack[STACK_SIZE];
 
 static bool execution_order(const TCHAR *input, double *outval, TCHAR *outstring, int maxlen)
 {
@@ -558,7 +560,6 @@ static bool execution_order(const TCHAR *input, double *outval, TCHAR *outstring
                                         if (isstackstring(sc2)) {
                                             TCHAR *c = stacktostring(sc2);
                                             _tcscpy(vals, c);
-                                            xfree(c);
                                         }
                                         val = stacktoval(sc2);
                                }
@@ -759,7 +760,11 @@ int calc(const TCHAR *input, double *outval, TCHAR *outstring, int maxlen)
     if (outstring) {
         outstring[0] = 0;
     }
-       if (parse_values(input, output2)) {
+    for (int i = 0; i < STACK_SIZE; i++) {
+        struct calcstack *s = &stack[i];
+        memset(s, 0, sizeof(struct calcstack));
+    }   
+    if (parse_values(input, output2)) {
                if(shunting_yard(output2, output))    {
                        calc_log ((_T("RPN OUT: %s\n"), output));
                        if(!execution_order(output, outval, outstring, maxlen)) {