Fix: make sure there is enough ram available

This commit is contained in:
Dorian Zedler 2022-07-20 12:22:43 +02:00
parent 81bcf3ea87
commit ace85e64a0
Signed by: dorian
GPG Key ID: 989DE36109AFA354
4 changed files with 15 additions and 2 deletions

View File

@ -1 +0,0 @@

View File

@ -9,6 +9,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/sysinfo.h>
/**
* @brief Open a file and load its stats

View File

@ -33,7 +33,18 @@ uint8_t* read_file(const char* path, size_t* size) {
return NULL;
}
struct sysinfo info;
int r = sysinfo(&info);
if (r != 0 || info.freeram < (unsigned long)statOfFile.st_size * 2) {
errno = ENOMEM;
return NULL;
}
uint8_t* data = malloc(statOfFile.st_size);
if (data == NULL) {
fclose(f);
return NULL;
}
size_t bytesRead =
fread(data, statOfFile.st_blksize, statOfFile.st_blocks, f);

View File

@ -138,8 +138,10 @@ int main(int argc, char** argv) {
char hash[32];
if (!calculate_hash(c, hash)) {
if (errno != 0) {
fprintf(stderr, "\n\033[0;31mAn error occured: %s\033[0m\n",
fprintf(stderr, "\n\033[1;31mAn error occured: %s\033[0m\n",
strerror(errno));
if (errno == ENOMEM)
fprintf(stderr, "\033[1;36mPlease try to use -V2!\033[0m\n");
}
return EXIT_FAILURE;
}