From c532661d29e7ea167dca585b1dcf5c9bb1527822 Mon Sep 17 00:00:00 2001 From: Jonathan Kamens Date: Fri, 29 Sep 2023 14:21:20 -0400 Subject: [PATCH] Add missing error-checking for return value of fread Several calls to fread were missing checks to ensure that the expected amount of data was read. --- src/cache.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/cache.c b/src/cache.c index 672752e..efa1f46 100644 --- a/src/cache.c +++ b/src/cache.c @@ -163,15 +163,11 @@ static int Meta_read(Cache *cf) return EIO; } - fread(&cf->time, sizeof(long), 1, fp); - fread(&cf->content_length, sizeof(off_t), 1, fp); - fread(&cf->blksz, sizeof(int), 1, fp); - fread(&cf->segbc, sizeof(long), 1, fp); - - /* - * Error checking for fread - */ - if (ferror(fp)) { + if (sizeof(long) != fread(&cf->time, sizeof(long), 1, fp) || + sizeof(off_t) != fread(&cf->content_length, sizeof(off_t), 1, fp) || + sizeof(int) != fread(&cf->blksz, sizeof(int), 1, fp) || + sizeof(long) != fread(&cf->segbc, sizeof(long), 1, fp) || + ferror(fp)) { lprintf(error, "error reading core metadata!\n"); return EIO; }