diff --git a/include/corrupter.h b/include/corrupter.h index c4dcfd1..4061c5d 100644 --- a/include/corrupter.h +++ b/include/corrupter.h @@ -58,6 +58,6 @@ typedef struct _corrupter_result Corrupter_Result; #define FLIP_BIT(x, n) ((x) ^ (1 << (n))) /* functions definitions */ -Corrupter_Result* corrupt_file(Corrupter_Param* param); +Corrupter_Result corrupt_file(Corrupter_Param* param); #endif /* _CORRUPTER_H */ diff --git a/src/corrupter.c b/src/corrupter.c index abddec5..c32e6c4 100644 --- a/src/corrupter.c +++ b/src/corrupter.c @@ -11,7 +11,7 @@ enum Interoperation_Result { Corrupter_Param* param, Corrupter_Result* result, FILE* file, \ file_offset_t i #define Interoperation_Share_Vars \ - param, result, file, \ + param, &result, file, \ i /* macros: lambdas */ @@ -19,7 +19,7 @@ enum Interoperation_Result { if (corrupt_byte(Interoperation_Share_Vars) == INTEROPERATION_ERROR) { \ PERROR_MACRO("corrupt_byte"); \ \ - result->error = true; \ + result.error = true; \ return result; \ } @@ -95,11 +95,12 @@ enum Interoperation_Result corrupt_byte(Interoperation_Input_Vars) { return INTEROPERATION_SUCCESS; } -Corrupter_Result* corrupt_file(Corrupter_Param* param) { - Corrupter_Result* result = malloc(sizeof(Corrupter_Result)); - - if (result == NULL) - return NULL; +Corrupter_Result corrupt_file(Corrupter_Param* param) { + Corrupter_Result result = { + .error = false, + .file_size = 0 /* undefined */, + .damaged_bytes = 0, + }; // copy and cast parameters FILE* file = param->file; @@ -108,7 +109,7 @@ Corrupter_Result* corrupt_file(Corrupter_Param* param) { if (FSEEK_MACRO(file, FILE_OFFSET_C(0), SEEK_SET) != 0 || FTELL_MACRO(file) != 0) { errno = ESPIPE; - result->error = true; + result.error = true; return result; } @@ -133,7 +134,7 @@ Corrupter_Result* corrupt_file(Corrupter_Param* param) { if (boundaries.invalid_file) { ERROR_MSG(failed_function, "Invalid file"); - result->error = true; + result.error = true; return result; } @@ -146,8 +147,7 @@ Corrupter_Result* corrupt_file(Corrupter_Param* param) { } // output data to result - result->file_size = end - start; - result->damaged_bytes = 0; + result.file_size = end - start; // initialize the PRNG mt_seed(param->seed); @@ -166,7 +166,5 @@ Corrupter_Result* corrupt_file(Corrupter_Param* param) { puts(" [OK]"); } - result->error = false; - return result; } diff --git a/src/main.c b/src/main.c index 48fd6d3..d6552b8 100644 --- a/src/main.c +++ b/src/main.c @@ -400,16 +400,9 @@ int main(int argc, char** argv) { .type = file_type }; - Corrupter_Result* result = corrupt_file(¶m); + Corrupter_Result result = corrupt_file(¶m); - if (result == NULL) { - PERROR_MACRO("corrupt_file memory allocation"); - - fclose(file); - - return EXIT_FAILURE; - } else if (result->error) { - free(result); + if (result.error) { fclose(file); return EXIT_FAILURE; @@ -417,8 +410,8 @@ int main(int argc, char** argv) { putchar('\n'); - if (result->damaged_bytes) { - size_t dmg = result->damaged_bytes, fsize = result->file_size, + if (result.damaged_bytes) { + size_t dmg = result.damaged_bytes, fsize = result.file_size, passes = param.passes; printf("Byte hit counter: %" PRIuMAX " / %" PRIuMAX " = %.3Lf%%\n", (uintmax_t) dmg, @@ -427,8 +420,6 @@ int main(int argc, char** argv) { } else puts("No bytes were damaged"); - free(result); - // finish working with the file fclose(file);