From: Kevin Day Date: Sat, 9 Apr 2022 05:18:25 +0000 (-0500) Subject: Bugfix: Invalid memory deletion. X-Git-Tag: 0.5.10~221 X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=3448c3cdf856f0b46114281235807be2e39c72c5;p=fll Bugfix: Invalid memory deletion. The memory is being incorrectly deleted for multiple reasons. 1) The type is not f_string_t, it is struct dirent. 2) The index within a listing is already a pointer. 3) The listing itself is already a pointer. 4) Remove extra delete commands. 5) The memory is not being deleted on certain errors. These problems have been exposed by the unit tests. Also replace macro_f_memory_structure_increment() with f_string_dynamics_increase(). --- diff --git a/level_0/f_directory/c/directory.c b/level_0/f_directory/c/directory.c index de24798..a2b88f3 100644 --- a/level_0/f_directory/c/directory.c +++ b/level_0/f_directory/c/directory.c @@ -168,8 +168,6 @@ extern "C" { } struct dirent **listing = 0; - size_t i = 0; - f_array_length_t size = 0; f_status_t status = F_none; const size_t length = scandir(path.string, &listing, filter, sort); @@ -180,38 +178,49 @@ extern "C" { return F_status_set_error(F_failure); } + size_t i = 0; + f_array_length_t size = 0; + for (; i < length; ++i) { size = strnlen(listing[i]->d_name, F_directory_name_max_d); // There is no reason to include "." and ".." in the directory listing. - if (!strncmp(listing[i]->d_name, f_directory_back_s.string, f_directory_back_s.used + 1) || !strncmp(listing[i]->d_name, f_directory_current_s.string, f_directory_current_s.used + 1)) { - f_memory_delete(size, sizeof(f_string_t), (void **) & listing[i]); + if (!strncmp(listing[i]->d_name, f_directory_back_s.string, f_directory_back_s.used + 1) || !strncmp(listing[i]->d_name, f_directory_current_s.string, f_directory_current_s.used + 1)) { + f_memory_delete(size, sizeof(struct dirent), (void *) listing[i]); continue; } - if (names->used == names->size) { - macro_f_memory_structure_increment(status, (*names), 1, F_directory_default_allocation_step_d, macro_f_string_dynamics_t_resize, F_array_too_large); - if (F_status_is_error(status)) break; + status = f_string_dynamics_increase(F_directory_default_allocation_step_d, names); + + if (F_status_is_error(status)) { + for (; i < length; ++i) { + f_memory_delete(size, sizeof(struct dirent), (void *) listing[i]); + } // for + + break; } names->array[names->used].used = 0; - status = f_string_dynamic_increase_by(size, &names->array[names->used]); - if (F_status_is_error(status)) break; + status = f_string_dynamic_increase_by(size + 1, &names->array[names->used]); + + if (F_status_is_error(status)) { + for (; i < length; ++i) { + f_memory_delete(size, sizeof(struct dirent), (void *) listing[i]); + } // for + + break; + } memcpy(names->array[names->used].string, listing[i]->d_name, sizeof(f_char_t) * size); names->array[names->used++].used = size; - f_memory_delete(size, sizeof(f_string_t), (void **) & listing[i]); - } // for - - for (; i < length; ++i) { - f_memory_delete(size, sizeof(f_string_t), (void **) & listing[i]); + 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;