]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
wc: ensure we update file offset
authorPádraig Brady <P@draigBrady.com>
Sun, 5 Feb 2023 19:52:31 +0000 (19:52 +0000)
committerPádraig Brady <P@draigBrady.com>
Sat, 8 Apr 2023 11:19:40 +0000 (12:19 +0100)
* src/wc.c (wc): Update the offset when not reading,
and do read if we can't update the offset.
* tests/misc/wc-proc.sh: Add a test case.
* NEWS: Mention the bug fix.
Fixes https://bugs.gnu.org/61300

NEWS
src/wc.c
tests/misc/wc-proc.sh

diff --git a/NEWS b/NEWS
index 09dba0b80546d278acdd2646caed1f6bfede6481..9d7836d7b3e30e35bacc175535eb7f1477d7681c 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -27,6 +27,10 @@ GNU coreutils NEWS                                    -*- outline -*-
   wc will now diagnose if any total counts have overflowed.
   [This bug was present in "the beginning".]
 
+  `wc -c` will again correctly update the read offset of inputs.
+  Previously it deduced the size of inputs while leaving the offset unchanged.
+  [bug introduced in coreutils-8.27]
+
 ** Changes in behavior
 
   'cp -n' and 'mv -n' now issue an error diagnostic if skipping a file,
index 801396a15087e2535309688e1e155f9381d597c8..becceda987692fd8319ade1e6432ee9d1bcaa0e7 100644 (file)
--- a/src/wc.c
+++ b/src/wc.c
@@ -450,7 +450,10 @@ wc (int fd, char const *file_x, struct fstatus *fstatus, off_t current_pos)
                  beyond the end of the file.  As in the example above.  */
 
               bytes = end_pos < current_pos ? 0 : end_pos - current_pos;
-              skip_read = true;
+              if (bytes && 0 <= lseek (fd, bytes, SEEK_CUR))
+                skip_read = true;
+              else
+                bytes = 0;
             }
           else
             {
index 5eb43b982f200767e372239f899888d857a362a5..2307f2c383b479f84e58603a8663113abc95cf1f 100755 (executable)
@@ -42,6 +42,18 @@ cat <<\EOF > exp
 EOF
 compare exp out || fail=1
 
+# Ensure we update the offset even when not reading,
+# which wasn't the case from coreutils-8.27 to coreutils-9.2
+{ wc -c; wc -c; } < no_read >  out || fail=1
+{ wc -c; wc -c; } < do_read >> out || fail=1
+cat <<\EOF > exp
+2
+0
+1048576
+0
+EOF
+compare exp out || fail=1
+
 # Ensure we don't read too much when reading,
 # as was the case on 32 bit systems
 # from coreutils-8.24 to coreutils-9.1