GNU bug report logs - #11631
Head command does not position file pointer correctly for negative line count

Previous Next

Package: coreutils;

Reported by: Anoop Sharma <sendtoanoop <at> gmail.com>

Date: Tue, 5 Jun 2012 09:41:01 UTC

Severity: normal

Done: Jim Meyering <jim <at> meyering.net>

Bug is archived. No further changes may be made.

Full log


View this message in rfc822 format

From: Jim Meyering <jim <at> meyering.net>
To: Anoop Sharma <sendtoanoop <at> gmail.com>
Cc: 11631 <at> debbugs.gnu.org
Subject: bug#11631: closed (Re: bug#11631: Head command does not position file pointer correctly for negative line count)
Date: Wed, 06 Jun 2012 10:02:18 +0200
Anoop Sharma wrote:
> 1. The comment in code - "Don't bother testing for failure for such a
> small amount. Any failure will be detected upon close." may be
> re-looked too, since we are now lseeking after it.
>
> What if we change plain fwrite to:
>       if (fwrite (buffer, 1, n + 1, stdout) < (n + 1))
>             error (EXIT_FAILURE, errno, _("write error"));
>
> 2. Maybe using lseek with SEEK_CUR in place of SEEK_SET would reflect
> logic better.

Regarding #1, thanks for the suggestion.  You're right.

I've written a complete patch in your name, below, adding a commit
log and removing the unnecessary parentheses around "n + 1".
In general, I much prefer a patch to a suggestion like that.
The advantage to you is that if you describe precisely enough
(usually requires a patch), then you're listed as the author
of the commit.  If you also take the time to write a commit
log entry like I did below, then that's even better.
See the file, HACKING, for instructions on how to do that
as well as general contribution guidelines.

Regarding #2, please be precise and write/post a patch.


From 7fcfa754f1ff92d4fed1495a05574b8e47c3b4fb Mon Sep 17 00:00:00 2001
From: Anoop Sharma <sendtoanoop <at> gmail.com>
Date: Wed, 6 Jun 2012 09:42:09 +0200
Subject: [PATCH] head: diagnose write failure immediately, given new lseek
 use

Inserting the lseek call (commit v8.17-13-g295ee52) rendered
inaccurate the comment/code that refrained from diagnosing the
failure of the just-preceding fwrite.
* src/head.c (elide_tail_lines_seekable): Remove now-erroneous
comment, and diagnose fwrite failure.
---
 src/head.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/src/head.c b/src/head.c
index c13c064..ffe7c74 100644
--- a/src/head.c
+++ b/src/head.c
@@ -663,10 +663,9 @@ elide_tail_lines_seekable (const char *pretty_filename, int fd,
                 }

               /* Output the initial portion of the buffer
-                 in which we found the desired newline byte.
-                 Don't bother testing for failure for such a small amount.
-                 Any failure will be detected upon close.  */
-              fwrite (buffer, 1, n + 1, stdout);
+                 in which we found the desired newline byte.  */
+              if (fwrite (buffer, 1, n + 1, stdout) < n + 1)
+                error (EXIT_FAILURE, errno, _("write error"));

               /* Set file pointer to the byte after what we've output.  */
               if (lseek (fd, pos + n + 1, SEEK_SET) < 0)
--
1.7.11.rc1




This bug report was last modified 13 years and 46 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.