Skip to content

Commit 523a88a

Browse files
committed
Free memory in exit paths
.. the previous arrangement lead to compiler diagnostics when building with 'scan-build make', such as: libtvm/tvm_parser.c:218:12: warning: Potential leak of memory pointed to by 'args' return -1; ^
1 parent 110a02f commit 523a88a

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

libtvm/tvm_parser.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,19 @@ static int **tvm_parse_args(
137137
return args;
138138
}
139139

140+
/* This function frees the memory allocated by tvm_parse_args().
141+
*/
142+
static void tvm_free_args(int **args) {
143+
if(args) {
144+
for (int i = 0; args[i]; i++) {
145+
free(args[i]);
146+
}
147+
148+
}
149+
free(args);
150+
}
151+
152+
140153

141154

142155
/* This is a helper function that converts one instruction,
@@ -190,17 +203,21 @@ int tvm_parse_program(
190203
if (newptr != NULL) {
191204
vm->prog->instr = newptr;
192205
vm->prog->instr[vm->prog->num_instr - 1] = opcode;
193-
} else
206+
} else {
207+
tvm_free_args(args);
194208
return -1;
209+
}
195210

196211
newptr = realloc(
197212
vm->prog->args,
198213
sizeof(int **) * (vm->prog->num_instr+1));
199214

200215
if (newptr != NULL)
201216
vm->prog->args = (int ***)newptr;
202-
else
217+
else {
218+
tvm_free_args(args);
203219
return -1;
220+
}
204221

205222
vm->prog->args[vm->prog->num_instr - 1] = args;
206223
}

libtvm/tvm_preprocessor.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ static int process_includes(
2929

3030
if (!filp) {
3131
printf("Unable to open file \"%s\"\n", filename);
32+
free(temp_str);
3233
return -1;
3334
}
3435

0 commit comments

Comments
 (0)