From 3e37f5fb73ffd07a189701d3b79aa754d0e7010a Mon Sep 17 00:00:00 2001 From: Intel A80486DX2-66 Date: Thu, 22 Feb 2024 21:12:54 +0300 Subject: [PATCH] C: reverse-ramdisk.c: `tf_free`: prevent memory leaks --- c-programming/experiments/reverse-ramdisk.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/c-programming/experiments/reverse-ramdisk.c b/c-programming/experiments/reverse-ramdisk.c index 5e57bde..ff35c95 100644 --- a/c-programming/experiments/reverse-ramdisk.c +++ b/c-programming/experiments/reverse-ramdisk.c @@ -170,11 +170,13 @@ int tf_free(int ID) { // Reallocate memory for the temp_files array if (--num_temp_files > 0) { DBG_PRINT("num_temp_files = %zu\n", num_temp_files); - if ((temp_files = realloc(temp_files, num_temp_files - * sizeof(TempFile))) == NULL) { - LINE_FAIL(-2); + TempFile* new_temp_files_ptr = realloc(temp_files, num_temp_files * + sizeof(TempFile)); + if (new_temp_files_ptr == NULL) { + LINE_FAIL(-3); return -1; } + temp_files = new_temp_files_ptr; } temp_files[index].locked = false;