Skip to content

Commit 08c478b

Browse files
committed
COMMON: fix memory leak
1 parent 0d3248e commit 08c478b

File tree

5 files changed

+32
-24
lines changed

5 files changed

+32
-24
lines changed

src/common/brun.c

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,6 +1263,7 @@ int brun_create_task(const char *filename, byte *preloaded_bc, int libf) {
12631263
cp += sizeof(bc_lib_rec_t);
12641264
}
12651265
}
1266+
12661267
// build import-symbol table
12671268
if (prog_symcount) {
12681269
prog_symtable = (bc_symbol_rec_t *)malloc(prog_symcount * sizeof(bc_symbol_rec_t));
@@ -1271,21 +1272,22 @@ int brun_create_task(const char *filename, byte *preloaded_bc, int libf) {
12711272
cp += sizeof(bc_symbol_rec_t);
12721273
}
12731274
}
1275+
12741276
// create system stack
12751277
prog_stack_alloc = SB_EXEC_STACK_SIZE;
12761278
prog_stack = malloc(sizeof(stknode_t) * prog_stack_alloc);
12771279
prog_stack_count = 0;
12781280
prog_timer = NULL;
12791281

12801282
// create eval's stack
1281-
eval_size = 64;
1283+
eval_size = SB_EVAL_STACK_SIZE;
12821284
eval_stk = malloc(sizeof(var_t) * eval_size);
1285+
memset(eval_stk, 0, sizeof(var_t) * eval_size);
12831286
eval_sp = 0;
12841287

12851288
// initialize the rest tasks globals
12861289
prog_error = 0;
12871290
prog_line = 0;
1288-
12891291
prog_dp = data_org = hdr.data_ip;
12901292
prog_length = hdr.bc_count;
12911293
prog_source = cp;
@@ -1385,7 +1387,6 @@ int brun_create_task(const char *filename, byte *preloaded_bc, int libf) {
13851387
int exec_close_task() {
13861388
word i;
13871389
stknode_t node;
1388-
13891390
if (ctask->bytecode) {
13901391
// clean up - format list
13911392
free_format();
@@ -1402,15 +1403,14 @@ int exec_close_task() {
14021403
free(prog_stack);
14031404
// clean up - variables
14041405
for (i = 0; i < (int) prog_varcount; i++) {
1405-
int j, shared;
14061406
// do not free imported variables
1407-
shared = -1;
1407+
int shared = -1;
1408+
int j;
14081409
for (j = 0; j < prog_symcount; j++) {
1409-
if (prog_symtable[j].type == stt_variable) {
1410-
if (prog_symtable[j].var_id == i) {
1411-
shared = i;
1412-
break;
1413-
}
1410+
if (prog_symtable[j].type == stt_variable &&
1411+
prog_symtable[j].var_id == i) {
1412+
shared = j;
1413+
break;
14141414
}
14151415
}
14161416

@@ -1503,8 +1503,13 @@ void exec_sync_variables(int dir) {
15031503
activate_task(ps->task_id);
15041504
vp = tvar[us->vid];
15051505

1506+
// pointer assignment (shared var_t pointer)
15061507
activate_task(tid);
1507-
tvar[ps->var_id] = vp; // pointer assignment (shared var_t pointer)
1508+
if (tvar[ps->var_id] != vp) {
1509+
v_free(tvar[ps->var_id]);
1510+
free(tvar[ps->var_id]);
1511+
}
1512+
tvar[ps->var_id] = vp;
15081513
} else {
15091514
activate_task(tid);
15101515
vp = tvar[ps->var_id];

src/common/eval.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -724,9 +724,12 @@ static inline void eval_push(var_t *r) {
724724
// expression-stack resize
725725
eval_sp++;
726726
if (eval_sp == eval_size) {
727-
eval_size += 64;
727+
eval_size += SB_EVAL_STACK_SIZE;
728728
eval_stk = realloc(eval_stk, sizeof(var_t) * eval_size);
729-
// rt_raise("EVAL: STACK OVERFLOW");
729+
int i;
730+
for (i = eval_sp; i < eval_size; i++) {
731+
v_init(&eval_stk[i]);
732+
}
730733
}
731734
}
732735

src/common/scan.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4003,8 +4003,7 @@ void comp_preproc_unit_path(char *p) {
40034003
*up++ = *p++;
40044004
}
40054005
*up = '\0';
4006-
sprintf(comp_bc_temp, "UNITPATH=%s", upath);
4007-
putenv(strdup(comp_bc_temp));
4006+
setenv("UNITPATH", upath, 1);
40084007
}
40094008
}
40104009
}
@@ -4365,7 +4364,7 @@ byte_code comp_create_bin() {
43654364

43664365
// unit header
43674366
memcpy(&uft.sign, "SBUn", 4);
4368-
uft.version = 1;
4367+
uft.version = SB_DWORD_VER;
43694368

43704369
strcpy(uft.base, comp_unit_name);
43714370
uft.sym_count = comp_expcount;

src/common/sys.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ typedef long int var_int_t;
8989
#define SB_KEYWORD_SIZE 128
9090
#define SB_SOURCELINE_SIZE 65536 // compiler
9191
#define SB_TEXTLINE_SIZE 8192 // RTL
92-
#define SB_EXEC_STACK_SIZE 256 // executor's stack size
92+
#define SB_EXEC_STACK_SIZE 256 // executor's stack size
93+
#define SB_EVAL_STACK_SIZE 16 // evaluation stack size
9394
#define SB_PI 3.14159265358979323846
9495

9596
// STD MACROS

src/common/units.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,8 @@ int open_unit(const char *file) {
142142
}
143143

144144
// compilation required
145-
if (comp_rq) {
146-
if (!comp_compile(bas_file)) {
147-
return -1;
148-
}
145+
if (comp_rq && !comp_compile(bas_file)) {
146+
return -1;
149147
}
150148

151149
// open unit
@@ -155,10 +153,12 @@ int open_unit(const char *file) {
155153
}
156154

157155
// read file header
158-
read(h, &u.hdr, sizeof(unit_file_t));
159-
if (memcmp(&u.hdr.sign, "SBUn", 4) != 0) {
156+
int nread = read(h, &u.hdr, sizeof(unit_file_t));
157+
if (nread != sizeof(unit_file_t) ||
158+
u.hdr.version != SB_DWORD_VER ||
159+
memcmp(&u.hdr.sign, "SBUn", 4) != 0) {
160160
close(h);
161-
return -2;
161+
return -1;
162162
}
163163

164164
// load symbol-table

0 commit comments

Comments
 (0)