From: Kevin Day Date: Sat, 9 Apr 2022 05:42:10 +0000 (-0500) Subject: Bugfix: Memory handling related to or exposed by f_directory unit tests. X-Git-Tag: 0.5.10~219 X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=a284929604aa907beeb513dbb94c0e5fc9f55c0a;p=fll Bugfix: Memory handling related to or exposed by f_directory unit tests. Replace a double pointer cast with a single pointer cast. Just in case, if the struct dirent listing is allocated, free it when scandir() returns -1. The entire struct dirent pointers do not need to be allocated as I thought. It seems these are copied inside scandir() where the memory deletes inside that function did not affect the mock pointer from the caller. This resulted in a minor memory leak in the unit test program. Replace the outermost struct dirent malloc with just a struct dirent array in these cases. --- diff --git a/level_0/f_directory/c/directory.c b/level_0/f_directory/c/directory.c index a2b88f3..5c51755 100644 --- a/level_0/f_directory/c/directory.c +++ b/level_0/f_directory/c/directory.c @@ -173,6 +173,10 @@ extern "C" { const size_t length = scandir(path.string, &listing, filter, sort); if (length == -1) { + if (listing) { + f_memory_delete(1, sizeof(struct dirent *), (void *) listing); + } + if (errno == ENOMEM) return F_status_set_error(F_memory_not); return F_status_set_error(F_failure); @@ -220,7 +224,7 @@ extern "C" { f_memory_delete(size, sizeof(struct dirent), (void *) listing[i]); } // for - f_memory_delete(1, sizeof(struct dirent *), (void **) listing); + f_memory_delete(1, sizeof(struct dirent *), (void *) listing); if (F_status_is_error(status)) { return status; diff --git a/level_0/f_directory/tests/unit/c/test-directory-list.c b/level_0/f_directory/tests/unit/c/test-directory-list.c index c8fd80c..f4f127c 100644 --- a/level_0/f_directory/tests/unit/c/test-directory-list.c +++ b/level_0/f_directory/tests/unit/c/test-directory-list.c @@ -69,8 +69,7 @@ void test__f_directory_list__returns_directory_empty(void **state) { const f_string_static_t path = macro_f_string_static_t_initialize("test", 0, 4); { - // The scandir() allocates the entire struct dirent. - struct dirent **directories = (struct dirent **) malloc(sizeof(struct dirent *)); + struct dirent *directories[0]; memset(directories, 0, sizeof(struct dirent *)); @@ -92,9 +91,9 @@ void test__f_directory_list__works(void **state) { const f_string_static_t path = macro_f_string_static_t_initialize("test", 0, 4); { - // The scandir() allocates the entire struct dirent. - struct dirent **directories = (struct dirent **) malloc(sizeof(struct dirent *)); + struct dirent *directories[1]; + // The scandir() allocates each struct dirent. directories[0] = (struct dirent *) malloc(sizeof(struct dirent)); memset(directories[0], 0, sizeof(struct dirent));