When wc gets its list of files by reading from stdin, using the argument '--from-files0=-', it reuses the same fstatus struct for each file.

The problem is that the 'wc' function checks the 'failed' member of this struct and if it is <=0, it skips doing fstat on the file. The main loop doesn't reset this value between files, so only the first file has fstat done on it.

This can result in the 'wc' function seeking past the end of subsequent files and then over-reporting their byte counts.

See the attached patch, which resets the fstatus struct in between files when reading the file list from stdin.

Thanks,
- Bill Fraser
From 3be80bf2a90c5d9d227fa9c920e7a5e0e70f473b Mon Sep 17 00:00:00 2001
From: "William R. Fraser" <wfra...@codewise.org>
Date: Sun, 20 Mar 2016 17:44:09 -0700
Subject: [PATCH] wc: fix wrong byte counts when using --files-from0

When using '--files-from0=-', wc reuses the same fstatus struct for each
file. However, it doesn't reset the fstatus.failed member between files,
which causes wc to skip doing a fstat on all files after the first.

This can result in wc seeking past the end of subsequent files, and then
over-reporting the byte count.
---
 src/wc.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/src/wc.c b/src/wc.c
index 94cbaff..4548235 100644
--- a/src/wc.c
+++ b/src/wc.c
@@ -806,6 +806,9 @@ main (int argc, char **argv)
         ok = false;
       else
         ok &= wc_file (file_name, &fstatus[nfiles ? i : 0]);
+
+      if (!nfiles)
+        fstatus[0].failed = 1;
     }
  argv_iter_done:
 
-- 
2.7.3

Reply via email to