From f197d69933648c30804916938cd65e1652471bcb Mon Sep 17 00:00:00 2001 From: David Fiala Date: Wed, 7 Jan 2026 15:18:07 -0800 Subject: [PATCH] check fread result to get rid of -Wunused-result, making -Werror usable MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit warning: ignoring return value of ‘fread’ declared with attribute ‘warn_unused_result’ [-Wunused-result] --- example.c | 12 +++++++++++- mqjs.c | 12 +++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/example.c b/example.c index 5385ede..1943fed 100644 --- a/example.c +++ b/example.c @@ -241,7 +241,17 @@ static uint8_t *load_file(const char *filename, int *plen) buf_len = ftell(f); fseek(f, 0, SEEK_SET); buf = malloc(buf_len + 1); - fread(buf, 1, buf_len, f); + if (!buf) { + perror(filename); + exit(1); + } + for (size_t total = 0, n; total < (size_t)buf_len; total += n) { + n = fread(buf + total, 1, buf_len - total, f); + if (n == 0) { + perror(filename); + exit(1); + } + } buf[buf_len] = '\0'; fclose(f); if (plen) diff --git a/mqjs.c b/mqjs.c index 46ad953..fcc12ee 100644 --- a/mqjs.c +++ b/mqjs.c @@ -250,7 +250,17 @@ static uint8_t *load_file(const char *filename, int *plen) buf_len = ftell(f); fseek(f, 0, SEEK_SET); buf = malloc(buf_len + 1); - fread(buf, 1, buf_len, f); + if (!buf) { + perror(filename); + exit(1); + } + for (size_t total = 0, n; total < (size_t)buf_len; total += n) { + n = fread(buf + total, 1, buf_len - total, f); + if (n == 0) { + perror(filename); + exit(1); + } + } buf[buf_len] = '\0'; fclose(f); if (plen)