Skip to content

Commit

Permalink
Check and use return value of fread(), also check ftell()
Browse files Browse the repository at this point in the history
closes #25
  • Loading branch information
andy5995 committed Mar 20, 2024
1 parent f68a625 commit 60f5f20
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions canfigger.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,19 @@ read_entire_file(const char *filename)
FILE *fp = fopen(filename, "r");
if (!fp)
{
fprintf(stderr, "Failed to open %s:%s\n", filename, strerror(errno));
fprintf(stderr, "Failed to open %s: %s\n", filename, strerror(errno));
return NULL;
}

fseek(fp, 0, SEEK_END);
long file_size = ftell(fp);
if (file_size < 0)
{
fprintf(stderr, "Error getting the size of %s: %s\n", filename,
strerror(errno));
fclose(fp);
return NULL;
}
fseek(fp, 0, SEEK_SET);

char *buffer = malloc_wrap(file_size + 1);
Expand All @@ -290,17 +297,30 @@ read_entire_file(const char *filename)
return NULL;
}

fread(buffer, 1, file_size, fp);
buffer[file_size] = '\0';
size_t n_bytes = fread(buffer, 1, file_size, fp);

int r = ferror(fp);
clearerr(fp);
fclose(fp);
if (ferror(fp))
{
fprintf(stderr, "Error reading %s: %s\n", filename, strerror(errno));
free(buffer);
fclose(fp);
return NULL;
}

if (r == 0)
// Note that if the return value of ftell() is -1 this cast would be bad.
// However, above, the return value of ftell() is checked, and the function
// returns if the value is < 0
if (n_bytes == (size_t) file_size)
{
buffer[file_size] = '\0';
fclose(fp);
return buffer;
}

fprintf(stderr, "Error reading %s(%d)\n", filename, r);
free(buffer);
fprintf(stderr, "Partial read of %s: expected %ld bytes, got %zu bytes\n",
filename, file_size, n_bytes);
fclose(fp);
return NULL;
}

Expand Down

0 comments on commit 60f5f20

Please sign in to comment.