summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPádraig Brady <P@draigBrady.com>2013-07-09 18:45:34 +0100
committerPádraig Brady <P@draigBrady.com>2013-07-12 00:25:42 +0100
commit5fdb50825f598035d52ea8a26be7611e1a906703 (patch)
tree837fb75d4ebcae756f0289713b74df1da08c37e3
parent90e0e9bf58fd5c9b6aa86c22c6351d830ed1c6eb (diff)
downloadcoreutils-5fdb50825f598035d52ea8a26be7611e1a906703.tar.xz
head: avoid redundant allocations when reading empty files
* src/head.c (elide_tail_lines_file): For seekable empty files, or seekable files where the current offset is after the end of the file, return immediately. Previously the short circuit code could not be reached due to logic error. Spotted by coverity.
-rw-r--r--src/head.c9
1 files changed, 4 insertions, 5 deletions
diff --git a/src/head.c b/src/head.c
index 00e1be17c..5411a3c0e 100644
--- a/src/head.c
+++ b/src/head.c
@@ -739,18 +739,17 @@ elide_tail_lines_file (const char *filename, int fd, uintmax_t n_elide)
off_t start_pos = lseek (fd, 0, SEEK_CUR);
off_t end_pos = lseek (fd, 0, SEEK_END);
- if (0 <= start_pos && start_pos < end_pos)
+ if (0 <= start_pos && 0 <= end_pos)
{
- /* If the file is empty, we're done. */
- if (end_pos == 0)
+ /* If no data to read we're done. */
+ if (start_pos >= end_pos)
return true;
return elide_tail_lines_seekable (filename, fd, n_elide,
start_pos, end_pos);
}
- /* lseek failed or the end offset precedes start.
- Fall through. */
+ /* lseek failed, Fall through... */
}
return elide_tail_lines_pipe (filename, fd, n_elide);