All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Fix return value checking of fread() in iolog.c
@ 2018-04-04 23:18 Rebecca Cran
  2018-04-05  1:19 ` Jens Axboe
  0 siblings, 1 reply; 3+ messages in thread
From: Rebecca Cran @ 2018-04-04 23:18 UTC (permalink / raw)
  To: fio; +Cc: Jens Axboe, Rebecca Cran

According to http://pubs.opengroup.org/onlinepubs/7908799/xsh/fread.html
fread() returns a size_t, not ssize_t, and returns a value of 0 on
both eof and an error. Therefore, check both feof() and ferror() to
determine whether the call succeeded.
---
 iolog.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/iolog.c b/iolog.c
index 2b5eaf0c..bfafc032 100644
--- a/iolog.c
+++ b/iolog.c
@@ -978,7 +978,7 @@ int iolog_file_inflate(const char *file)
 	struct iolog_compress ic;
 	z_stream stream;
 	struct stat sb;
-	ssize_t ret;
+	size_t ret;
 	size_t total;
 	void *buf;
 	FILE *f;
@@ -1000,12 +1000,12 @@ int iolog_file_inflate(const char *file)
 	ic.seq = 1;
 
 	ret = fread(ic.buf, ic.len, 1, f);
-	if (ret < 0) {
+	if (ret == 0 && ferror(f)) {
 		perror("fread");
 		fclose(f);
 		free(buf);
 		return 1;
-	} else if (ret != 1) {
+	} else if (ferror(f) || (!feof(f) && ret != 1)) {
 		log_err("fio: short read on reading log\n");
 		fclose(f);
 		free(buf);
-- 
2.16.2.windows.1



^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] Fix return value checking of fread() in iolog.c
  2018-04-04 23:18 [PATCH] Fix return value checking of fread() in iolog.c Rebecca Cran
@ 2018-04-05  1:19 ` Jens Axboe
  2018-04-05  3:12   ` Rebecca Cran
  0 siblings, 1 reply; 3+ messages in thread
From: Jens Axboe @ 2018-04-05  1:19 UTC (permalink / raw)
  To: Rebecca Cran, fio

On 4/4/18 5:18 PM, Rebecca Cran wrote:
> According to http://pubs.opengroup.org/onlinepubs/7908799/xsh/fread.html
> fread() returns a size_t, not ssize_t, and returns a value of 0 on
> both eof and an error. Therefore, check both feof() and ferror() to
> determine whether the call succeeded.

Thanks Rebecca, applied.

-- 
Jens Axboe



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] Fix return value checking of fread() in iolog.c
  2018-04-05  1:19 ` Jens Axboe
@ 2018-04-05  3:12   ` Rebecca Cran
  0 siblings, 0 replies; 3+ messages in thread
From: Rebecca Cran @ 2018-04-05  3:12 UTC (permalink / raw)
  To: Jens Axboe, fio

On 4/4/2018 7:19 PM, Jens Axboe wrote:
> On 4/4/18 5:18 PM, Rebecca Cran wrote:
>> According to http://pubs.opengroup.org/onlinepubs/7908799/xsh/fread.html
>> fread() returns a size_t, not ssize_t, and returns a value of 0 on
>> both eof and an error. Therefore, check both feof() and ferror() to
>> determine whether the call succeeded.
> Thanks Rebecca, applied.

By the way, this fixes the --inflate-log option on Windows - it would 
previously error out reporting a short read.

-- 
Rebecca


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2018-04-05  3:12 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-04-04 23:18 [PATCH] Fix return value checking of fread() in iolog.c Rebecca Cran
2018-04-05  1:19 ` Jens Axboe
2018-04-05  3:12   ` Rebecca Cran

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.