From 4158335c953ab0d41c30e703a02b59df112cbfc9 Mon Sep 17 00:00:00 2001 From: Zane Beckwith Date: Wed, 27 Sep 2017 09:05:22 -0500 Subject: [PATCH] Fix resource leak in examples/file_utils.h. --- examples/file_utils.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/examples/file_utils.h b/examples/file_utils.h index 47caa9c..74fabe5 100644 --- a/examples/file_utils.h +++ b/examples/file_utils.h @@ -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; } @@ -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; }