Quellcode durchsuchen

Bugfixes

Memory leak : now free stack
mem_sz op : bad size was given to bzero
sub op : handling underflow case and preparing signed int handling
Yann Weber vor 6 Jahren
Ursprung
Commit
bf6a13f14a
2 geänderte Dateien mit 18 neuen und 4 gelöschten Zeilen
  1. 16
    4
      libs/turmit.c
  2. 2
    0
      libs/turmit.h

+ 16
- 4
libs/turmit.c Datei anzeigen

@@ -10,13 +10,17 @@ TURMIT_OP(mem_sz)
10 10
 	new_sz = new_sz<2?2:new_sz;
11 11
 
12 12
 	new = realloc(turmit->stack, sizeof(turmit_int) * new_sz);
13
-	if(!new) { return; }
13
+	if(!new) {
14
+		perror("Unable to reallocate memory for stack");
15
+		return; 
16
+	}
14 17
 	turmit->stack = new;
15 18
 	turmit->stack_sz = new_sz;
16 19
 	turmit->stack_cur %= new_sz;
17 20
 	if(old_sz < new_sz)
18 21
 	{
19
-		bzero(turmit->stack + old_sz, new_sz - old_sz);
22
+		bzero(turmit->stack + old_sz,
23
+			sizeof(turmit_int) * (new_sz - old_sz));
20 24
 	}
21 25
 }
22 26
 
@@ -29,7 +33,14 @@ TURMIT_OP(add)
29 33
 TURMIT_OP(sub)
30 34
 {
31 35
 	turmit_int a = SPOP(turmit), b = SPOP(turmit);
32
-	SPUSH(turmit, b - a);
36
+	if(turmit->flags & TURMIT_SIGNED)
37
+	{
38
+		fprintf(stderr, "Signed int not implemented\n");
39
+	}
40
+	else
41
+	{
42
+		SPUSH(turmit, a>b?(turmit->int_max - a) + b:b - a);
43
+	}
33 44
 }
34 45
 
35 46
 TURMIT_OP(bin_and)
@@ -157,9 +168,9 @@ turmit_t *turmit_init(turmit_t *turmit, ssize_t stack_sz, ssize_t int_max,
157 168
 	turmit->op_end = 0;
158 169
 	turmit->stack_sz = stack_sz;
159 170
 	turmit->stack_cur = turmit->stack_sz - 1;
160
-	turmit->stack = malloc(sizeof(turmit_int) * turmit->stack_sz);
161 171
 	turmit->err = 0;
162 172
 	turmit->err_str = NULL;
173
+	turmit->stack = malloc(sizeof(turmit_int) * turmit->stack_sz);
163 174
 
164 175
 	if(!turmit->stack)
165 176
 	{
@@ -211,6 +222,7 @@ turmit_t *turmit_copy(turmit_t *turmit)
211 222
 
212 223
 void turmit_clean(turmit_t *turmit)
213 224
 {
225
+	if(turmit->stack) { free(turmit->stack); }
214 226
 	if(turmit->err_str) { free(turmit->err_str); }
215 227
 	if(turmit->expr) { free(turmit->expr); }
216 228
 	if(turmit->op_expr) { free(turmit->op_expr); }

+ 2
- 0
libs/turmit.h Datei anzeigen

@@ -30,6 +30,8 @@ typedef void (*turmit_op_f)(turmit_t*);
30 30
 
31 31
 #define TURMIT_LAZY		0
32 32
 #define TURMIT_AUTOCOMP		1
33
+#define TURMIT_UNSIGNED		0
34
+#define TURMIT_SIGNED		2
33 35
 
34 36
 #define TURMIT_OK		0
35 37
 #define TURMIT_ERROR		-1

Laden…
Abbrechen
Speichern