1
0
mirror of https://git.disroot.org/80486DX2-66/polonium.git synced 2024-12-25 23:00:07 +05:30

corrupter.c: corrupt_file(): store result on stack rather than heap

This commit is contained in:
Intel A80486DX2-66 2024-11-20 00:48:13 +03:00
parent 290467bcd1
commit 65042ff90b
Signed by: 80486DX2-66
GPG Key ID: 83631EF27054609B
3 changed files with 16 additions and 27 deletions

View File

@ -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 */

View File

@ -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;
}

View File

@ -400,16 +400,9 @@ int main(int argc, char** argv) {
.type = file_type
};
Corrupter_Result* result = corrupt_file(&param);
Corrupter_Result result = corrupt_file(&param);
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);