Skip to content

Commit fe5b1f9

Browse files
authored
Merge pull request #287 from HeatCrab/fix-fixture-memory-issue
Fix memory leaks in dudect
2 parents f53314e + 00cd93a commit fe5b1f9

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

dudect/fixture.c

+11-4
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ static bool doit(int mode)
187187
int64_t *percentiles = calloc(NUM_PERCENTILES, sizeof(int64_t));
188188

189189
if (!before_ticks || !after_ticks || !exec_times || !classes ||
190-
!input_data) {
190+
!input_data || !percentiles) {
191191
die();
192192
}
193193

@@ -213,18 +213,24 @@ static void init_once(void)
213213
{
214214
init_dut();
215215
for (size_t i = 0; i < DUDECT_TESTS; i++) {
216-
ctxs[i] = malloc(sizeof(t_context_t));
217-
t_init(ctxs[i]);
216+
/* Check if ctxs[i] is unallocated to prevent repeated memory
217+
* allocations.
218+
*/
219+
if (!ctxs[i]) {
220+
ctxs[i] = malloc(sizeof(t_context_t));
221+
t_init(ctxs[i]);
222+
}
218223
}
219224
}
220225

221226
static bool test_const(char *text, int mode)
222227
{
223228
bool result = false;
224229

230+
init_once();
231+
225232
for (int cnt = 0; cnt < TEST_TRIES; ++cnt) {
226233
printf("Testing %s...(%d/%d)\n\n", text, cnt, TEST_TRIES);
227-
init_once();
228234
for (int i = 0; i < ENOUGH_MEASURE / (N_MEASURES - DROP_SIZE * 2) + 1;
229235
++i)
230236
result = doit(mode);
@@ -235,6 +241,7 @@ static bool test_const(char *text, int mode)
235241

236242
for (size_t i = 0; i < DUDECT_TESTS; i++) {
237243
free(ctxs[i]);
244+
ctxs[i] = NULL;
238245
}
239246

240247
return result;

0 commit comments

Comments
 (0)