From 03f7a5460a5650c12c4677971244413dd0e052a4 Mon Sep 17 00:00:00 2001 From: Intel A80486DX2-66 Date: Thu, 28 Dec 2023 00:07:29 +0300 Subject: [PATCH] reverse-ramdisk.c: fix segmentation fault, remove index check --- c-programming/reverse-ramdisk.c | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/c-programming/reverse-ramdisk.c b/c-programming/reverse-ramdisk.c index f36e5ea..233d8ed 100644 --- a/c-programming/reverse-ramdisk.c +++ b/c-programming/reverse-ramdisk.c @@ -93,11 +93,6 @@ int tf_alloc(size_t n, size_t type_size) { temp_file->file_path = strdup(file_path); temp_file->file = file; - // Add the temp file to the array - temp_files[num_temp_files] = *temp_file; - - num_temp_files++; - // Allocate/reallocate memory for the temp_files structure if (temp_files == NULL) temp_files = malloc(sizeof(TempFile)); @@ -109,15 +104,15 @@ int tf_alloc(size_t n, size_t type_size) { return -1; } + // Add the temp file to the array + temp_files[num_temp_files++] = *temp_file; + return temp_file->ID; } int tf_free(int ID) { size_t index = (size_t) ID; - if (index == -1) - return -1; - fclose(temp_files[index].file); // Delete the file @@ -148,9 +143,6 @@ int tf_free(int ID) { int tf_write(int ID, size_t offset, void* data, size_t data_size) { size_t index = (size_t) ID; - if (index == -1) - return -1; - // Check file handler for NULL FILE* file = temp_files[index].file; if (file == NULL)