Skip to content

Commit

Permalink
firmware-utils: mkdlinkfw: fix error handling
Browse files Browse the repository at this point in the history
fread() doesn't set errno, ferror need to be used to check for errors.
While at it, check if we read the expect number of elements.

Signed-off-by: Mathias Kresin <dev@kresin.me>
  • Loading branch information
mkresin committed Jan 13, 2019
1 parent e428b12 commit 0e78af2
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions tools/firmware-utils/src/mkdlinkfw-lib.c
Expand Up @@ -97,16 +97,16 @@ int read_to_buf(const struct file_info *fdata, char *buf)
{ {
FILE *f; FILE *f;
int ret = EXIT_FAILURE; int ret = EXIT_FAILURE;
size_t read;


f = fopen(fdata->file_name, "r"); f = fopen(fdata->file_name, "r");
if (f == NULL) { if (f == NULL) {
ERRS("could not open \"%s\" for reading", fdata->file_name); ERRS("could not open \"%s\" for reading", fdata->file_name);
goto out; goto out;
} }


errno = 0; read = fread(buf, fdata->file_size, 1, f);
fread(buf, fdata->file_size, 1, f); if (ferror(f) || read != 1) {
if (errno != 0) {
ERRS("unable to read from file \"%s\"", fdata->file_name); ERRS("unable to read from file \"%s\"", fdata->file_name);
goto out_close; goto out_close;
} }
Expand Down

0 comments on commit 0e78af2

Please sign in to comment.