From d65d2b5572a86946668df6c7c91581699d49ea4a Mon Sep 17 00:00:00 2001 From: Pablo de Lara Date: Thu, 14 Dec 2023 23:47:00 +0000 Subject: [PATCH] crc: [test] fix memory leak Signed-off-by: Pablo de Lara --- crc/crc16_t10dif_copy_test.c | 16 +++++++++++++--- crc/crc16_t10dif_test.c | 5 ++++- crc/crc32_funcs_test.c | 2 ++ crc/crc64_funcs_test.c | 2 ++ 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/crc/crc16_t10dif_copy_test.c b/crc/crc16_t10dif_copy_test.c index 7d0e92a..ae791e1 100644 --- a/crc/crc16_t10dif_copy_test.c +++ b/crc/crc16_t10dif_copy_test.c @@ -34,6 +34,7 @@ #include #include "crc.h" #include "crc_ref.h" +#include "test.h" #ifndef RANDOMS # define RANDOMS 20 @@ -116,16 +117,21 @@ int main(int argc, char *argv[]) int r = 0; int i; int len, tot; - u8 *src_raw, *dst_raw; + u8 *src_raw = NULL, *dst_raw = NULL; u8 *src, *dst; printf("Test crc16_t10dif_copy_test:\n"); src_raw = (u8 *) malloc(TEST_LEN); - dst_raw = (u8 *) malloc(TEST_LEN); - if (NULL == src_raw || NULL == dst_raw) { + if (NULL == src_raw) { printf("alloc error: Fail"); return -1; } + dst_raw = (u8 *) malloc(TEST_LEN); + if (NULL == dst_raw) { + printf("alloc error: Fail"); + aligned_free(src_raw); + return -1; + } src = src_raw; dst = dst_raw; @@ -179,5 +185,9 @@ int main(int argc, char *argv[]) #endif printf("Test done: %s\n", r ? "Fail" : "Pass"); + + free(src_raw); + free(dst_raw); + return r; } diff --git a/crc/crc16_t10dif_test.c b/crc/crc16_t10dif_test.c index 2a1631c..9407e37 100644 --- a/crc/crc16_t10dif_test.c +++ b/crc/crc16_t10dif_test.c @@ -60,7 +60,7 @@ int main(int argc, char *argv[]) int fail = 0; u32 r = 0; int i, s; - void *buf_raw; + void *buf_raw = NULL; unsigned char *buf; printf("Test crc16_t10dif_test "); @@ -191,5 +191,8 @@ int main(int argc, char *argv[]) if (fail) printf("\nFailed %d tests\n", fail); + if (buf) + aligned_free(buf_raw); + return fail; } diff --git a/crc/crc32_funcs_test.c b/crc/crc32_funcs_test.c index f8fe775..691d6ba 100644 --- a/crc/crc32_funcs_test.c +++ b/crc/crc32_funcs_test.c @@ -133,6 +133,8 @@ int main(int argc, char *argv[]) printf("CRC32 Tests all done: %s\n", fail ? "Fail" : "Pass"); + aligned_free(buf_alloc); + return fail; } diff --git a/crc/crc64_funcs_test.c b/crc/crc64_funcs_test.c index 9134366..ee1e695 100644 --- a/crc/crc64_funcs_test.c +++ b/crc/crc64_funcs_test.c @@ -129,6 +129,8 @@ int main(int argc, char *argv[]) printf("CRC64 Tests all done: %s\n", fail ? "Fail" : "Pass"); + aligned_free(buf_alloc); + return fail; }