Skip to content

Commit a3e2604

Browse files
pablodelaratkanteck
authored andcommitted
erasure_code: [test] fix memory leak
Signed-off-by: Pablo de Lara <[email protected]>
1 parent abd80d3 commit a3e2604

File tree

4 files changed

+181
-94
lines changed

4 files changed

+181
-94
lines changed

erasure_code/erasure_code_test.c

Lines changed: 64 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -215,13 +215,14 @@ static int gf_gen_decode_matrix(unsigned char *encode_matrix,
215215

216216
int main(int argc, char *argv[])
217217
{
218-
int re = 0;
218+
int re = -1;
219219
int i, j, p, rtest, m, k;
220220
int nerrs, nsrcerrs;
221221
void *buf;
222222
unsigned int decode_index[MMAX];
223-
unsigned char *temp_buffs[TEST_SOURCES], *buffs[TEST_SOURCES];
224-
unsigned char *encode_matrix, *decode_matrix, *invert_matrix, *g_tbls;
223+
unsigned char *temp_buffs[TEST_SOURCES] = { NULL }, *buffs[TEST_SOURCES] = { NULL };
224+
unsigned char *encode_matrix = NULL, *decode_matrix = NULL, *invert_matrix =
225+
NULL, *g_tbls = NULL;
225226
unsigned char src_in_err[TEST_SOURCES], src_err_list[TEST_SOURCES];
226227
unsigned char *recov[TEST_SOURCES];
227228

@@ -238,15 +239,15 @@ int main(int argc, char *argv[])
238239
for (i = 0; i < TEST_SOURCES; i++) {
239240
if (posix_memalign(&buf, 64, TEST_LEN)) {
240241
printf("alloc error: Fail");
241-
return -1;
242+
goto exit;
242243
}
243244
buffs[i] = buf;
244245
}
245246

246247
for (i = 0; i < TEST_SOURCES; i++) {
247248
if (posix_memalign(&buf, 64, TEST_LEN)) {
248249
printf("alloc error: Fail");
249-
return -1;
250+
goto exit;
250251
}
251252
temp_buffs[i] = buf;
252253
}
@@ -260,13 +261,15 @@ int main(int argc, char *argv[])
260261
if (encode_matrix == NULL || decode_matrix == NULL
261262
|| invert_matrix == NULL || g_tbls == NULL) {
262263
printf("Test failure! Error with malloc\n");
263-
return -1;
264+
goto exit;
264265
}
265266
// Pick a first test
266267
m = 9;
267268
k = 5;
268-
if (m > MMAX || k > KMAX)
269-
return -1;
269+
if (m > MMAX || k > KMAX) {
270+
re = -1;
271+
goto exit;
272+
}
270273

271274
// Make random data
272275
for (i = 0; i < k; i++)
@@ -295,7 +298,7 @@ int main(int argc, char *argv[])
295298
nerrs, nsrcerrs, k, m);
296299
if (re != 0) {
297300
printf("Fail to gf_gen_decode_matrix\n");
298-
return -1;
301+
goto exit;
299302
}
300303
// Pack recovery array as list of valid sources
301304
// Its order must be the same as the order
@@ -327,15 +330,18 @@ int main(int argc, char *argv[])
327330
dump(temp_buffs[k + i], 25);
328331
printf("orig :");
329332
dump(buffs[src_err_list[i]], 25);
330-
return -1;
333+
re = -1;
334+
goto exit;
331335
}
332336
}
333337

334338
// Pick a first test
335339
m = 9;
336340
k = 5;
337-
if (m > MMAX || k > KMAX)
338-
return -1;
341+
if (m > MMAX || k > KMAX) {
342+
re = -1;
343+
goto exit;
344+
}
339345

340346
// Make random data
341347
for (i = 0; i < k; i++)
@@ -363,7 +369,7 @@ int main(int argc, char *argv[])
363369
nerrs, nsrcerrs, k, m);
364370
if (re != 0) {
365371
printf("Fail to gf_gen_decode_matrix\n");
366-
return -1;
372+
goto exit;
367373
}
368374
// Pack recovery array as list of valid sources
369375
// Its order must be the same as the order
@@ -395,7 +401,8 @@ int main(int argc, char *argv[])
395401
dump(temp_buffs[k + i], 25);
396402
printf("orig :");
397403
dump(buffs[src_err_list[i]], 25);
398-
return -1;
404+
re = -1;
405+
goto exit;
399406
}
400407
}
401408

@@ -433,7 +440,7 @@ int main(int argc, char *argv[])
433440
src_in_err, nerrs, nsrcerrs, k, m);
434441
if (re != 0) {
435442
printf("Fail to gf_gen_decode_matrix\n");
436-
return -1;
443+
goto exit;
437444
}
438445
// Pack recovery array as list of valid sources
439446
// Its order must be the same as the order
@@ -468,7 +475,8 @@ int main(int argc, char *argv[])
468475
dump(buffs[src_err_list[i]], 25);
469476
printf("recov %d:", src_err_list[i]);
470477
dump(temp_buffs[k + i], 25);
471-
return -1;
478+
re = -1;
479+
goto exit;
472480
}
473481
}
474482
#ifdef TEST_VERBOSE
@@ -479,13 +487,17 @@ int main(int argc, char *argv[])
479487
// Run tests at end of buffer for Electric Fence
480488
k = 16;
481489
align = (LEN_ALIGN_CHK_B != 0) ? 1 : 16;
482-
if (k > KMAX)
483-
return -1;
490+
if (k > KMAX) {
491+
re = -1;
492+
goto exit;
493+
}
484494

485495
for (rows = 1; rows <= 16; rows++) {
486496
m = k + rows;
487-
if (m > MMAX)
488-
return -1;
497+
if (m > MMAX) {
498+
re = -1;
499+
goto exit;
500+
}
489501

490502
// Make random data
491503
for (i = 0; i < k; i++)
@@ -518,7 +530,7 @@ int main(int argc, char *argv[])
518530
src_in_err, nerrs, nsrcerrs, k, m);
519531
if (re != 0) {
520532
printf("Fail to gf_gen_decode_matrix\n");
521-
return -1;
533+
goto exit;
522534
}
523535
// Pack recovery array as list of valid sources
524536
// Its order must be the same as the order
@@ -558,7 +570,8 @@ int main(int argc, char *argv[])
558570
dump(temp_buffs[k + i], align);
559571
printf("orig :");
560572
dump(efence_buffs[src_err_list[i]], align);
561-
return -1;
573+
re = -1;
574+
goto exit;
562575
}
563576
}
564577
}
@@ -610,7 +623,7 @@ int main(int argc, char *argv[])
610623
src_in_err, nerrs, nsrcerrs, k, m);
611624
if (re != 0) {
612625
printf("Fail to gf_gen_decode_matrix\n");
613-
return -1;
626+
goto exit;
614627
}
615628
// Pack recovery array as list of valid sources
616629
// Its order must be the same as the order
@@ -645,7 +658,8 @@ int main(int argc, char *argv[])
645658
dump(ubuffs[src_err_list[i]], 25);
646659
printf("recov %d:", src_err_list[i]);
647660
dump(temp_ubuffs[k + i], 25);
648-
return -1;
661+
re = -1;
662+
goto exit;
649663
}
650664
}
651665

@@ -658,13 +672,15 @@ int main(int argc, char *argv[])
658672

659673
if (memcmp(buffs[i], temp_buffs[0], offset)) {
660674
printf("Fail rand ualign encode pad start\n");
661-
return -1;
675+
re = -1;
676+
goto exit;
662677
}
663678
if (memcmp
664679
(buffs[i] + offset + size, temp_buffs[0],
665680
PTR_ALIGN_CHK_B - offset)) {
666681
printf("Fail rand ualign encode pad end\n");
667-
return -1;
682+
re = -1;
683+
goto exit;
668684
}
669685
}
670686

@@ -673,13 +689,15 @@ int main(int argc, char *argv[])
673689
offset = temp_ubuffs[k + i] - temp_buffs[k + i];
674690
if (memcmp(temp_buffs[k + i], temp_buffs[0], offset)) {
675691
printf("Fail rand ualign decode pad start\n");
676-
return -1;
692+
re = -1;
693+
goto exit;
677694
}
678695
if (memcmp
679696
(temp_buffs[k + i] + offset + size, temp_buffs[0],
680697
PTR_ALIGN_CHK_B - offset)) {
681698
printf("Fail rand ualign decode pad end\n");
682-
return -1;
699+
re = -1;
700+
goto exit;
683701
}
684702
}
685703

@@ -723,7 +741,7 @@ int main(int argc, char *argv[])
723741
src_in_err, nerrs, nsrcerrs, k, m);
724742
if (re != 0) {
725743
printf("Fail to gf_gen_decode_matrix\n");
726-
return -1;
744+
goto exit;
727745
}
728746
// Pack recovery array as list of valid sources
729747
// Its order must be the same as the order
@@ -758,11 +776,26 @@ int main(int argc, char *argv[])
758776
dump(buffs[src_err_list[i]], 25);
759777
printf("recov %d:", src_err_list[i]);
760778
dump(temp_buffs[k + i], 25);
761-
return -1;
779+
re = -1;
780+
goto exit;
762781
}
763782
}
764783
}
765784

766785
printf("done EC tests: Pass\n");
767-
return 0;
786+
re = 0;
787+
788+
exit:
789+
for (i = 0; i < TEST_SOURCES; i++) {
790+
if (buffs[i])
791+
aligned_free(buffs[i]);
792+
if (temp_buffs[i])
793+
aligned_free(temp_buffs[i]);
794+
}
795+
free(encode_matrix);
796+
free(decode_matrix);
797+
free(invert_matrix);
798+
free(g_tbls);
799+
800+
return re;
768801
}

0 commit comments

Comments
 (0)