Fix: wrong filesize read

This commit is contained in:
Dorian Zedler 2022-07-20 15:51:29 +02:00
parent 72e2136ceb
commit 1c42f4036f
Signed by: dorian
GPG key ID: 989DE36109AFA354
2 changed files with 8 additions and 9 deletions

View file

@ -36,9 +36,9 @@ md2: ${OBJ}
t/%:
@mkdir -p t
@echo
@echo "${BLUE}=== Generating ${subst t/,,$@}MB of random data... ===${NC}"
@echo -e "${BLUE}=== Generating ${subst t/,,$@}MB of random data... ===${NC}"
dd if=/dev/random of=$@ bs=1M count=${subst t/,,$@} status=progress
@echo "${BLUE}=== done ===${NC}"
@echo -e "${BLUE}=== done ===${NC}"
@echo
benchmarks.csv: md2 ${TESTFILES}

View file

@ -35,22 +35,21 @@ uint8_t* read_file(const char* path, size_t* size) {
struct sysinfo info;
int r = sysinfo(&info);
if (r != 0 || info.freeram < (unsigned long)(statOfFile.st_blksize *
statOfFile.st_blocks * 2)) {
if (r != 0 || info.freeram < (unsigned long)(statOfFile.st_size * 2)) {
errno = ENOMEM;
return NULL;
}
uint8_t* data = malloc(statOfFile.st_blksize * statOfFile.st_blocks);
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);
// size_t bytesRead = fread(data, sizeof(uint8_t), statOfFile.st_size, f);
if (bytesRead != 0 && !feof(f)) {
fread(data, 1, statOfFile.st_size, f);
if (ferror(f)) {
fclose(f);
if (errno == 0) errno = EIO;
return NULL;