Skip to content

Commit

Permalink
Merge pull request #37 from zanebeckwith/fix-examples-file-leak
Browse files Browse the repository at this point in the history
Fix resource leak in examples/file_utils.h.
  • Loading branch information
Zane Beckwith authored Sep 27, 2017
2 parents 7663938 + 4158335 commit 7f0b3f5
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions examples/file_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ static int read_file_into_buffer(uint8_t *buffer, size_t bytes_to_read, const ch
if (NULL == ptr)
return -1;

if (fread(buffer, 1, bytes_to_read, ptr) != bytes_to_read)
return -1;
size_t bytes_read = fread(buffer, 1, bytes_to_read, ptr);

(void)fclose(ptr);

if (bytes_read != bytes_to_read)
return -1;

return 0;
}

Expand All @@ -42,10 +44,12 @@ static int write_buffer_to_file(const char *filename, uint8_t *buffer, size_t by
if (NULL == ptr)
return -1;

if (fwrite(buffer, 1, bytes_to_write, ptr) != bytes_to_write)
return -1;
size_t bytes_written = fwrite(buffer, 1, bytes_to_write, ptr);

(void)fclose(ptr);

if (bytes_written != bytes_to_write)
return -1;

return 0;
}

0 comments on commit 7f0b3f5

Please sign in to comment.