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 <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/sysinfo.h>
/** /**
* @brief Open a file and load its stats * @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; 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); uint8_t* data = malloc(statOfFile.st_size);
if (data == NULL) {
fclose(f);
return NULL;
}
size_t bytesRead = size_t bytesRead =
fread(data, statOfFile.st_blksize, statOfFile.st_blocks, f); fread(data, statOfFile.st_blksize, statOfFile.st_blocks, f);

View file

@ -138,8 +138,10 @@ int main(int argc, char** argv) {
char hash[32]; char hash[32];
if (!calculate_hash(c, hash)) { if (!calculate_hash(c, hash)) {
if (errno != 0) { 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)); strerror(errno));
if (errno == ENOMEM)
fprintf(stderr, "\033[1;36mPlease try to use -V2!\033[0m\n");
} }
return EXIT_FAILURE; return EXIT_FAILURE;
} }