summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJim Meyering <jim@meyering.net>2003-08-16 17:34:41 +0000
committerJim Meyering <jim@meyering.net>2003-08-16 17:34:41 +0000
commitf6008be14a876cbb13897b73e1feeaa0a7208aa5 (patch)
tree4b79b89b4bd3eda8a07cb5f5450298136124a70e /src
parentf4be51104a8e353795716a63268d0d32aa97de7a (diff)
downloadcoreutils-f6008be14a876cbb13897b73e1feeaa0a7208aa5.tar.xz
(tail_lines): Fix a potential (but very hard to exercise)
race condition bug. The bug would be triggered when tailing a file with file pointer not at beginning of file, and where the file was truncated to have a length of less than the initial offset at just the right moment (between the two lseek calls in this function).
Diffstat (limited to 'src')
-rw-r--r--src/tail.c9
1 files changed, 8 insertions, 1 deletions
diff --git a/src/tail.c b/src/tail.c
index 7d8378707..f4299c9b8 100644
--- a/src/tail.c
+++ b/src/tail.c
@@ -1184,7 +1184,7 @@ tail_lines (const char *pretty_filename, int fd, uintmax_t n_lines,
}
else
{
- off_t start_pos;
+ off_t start_pos = -1;
off_t end_pos;
/* Use file_lines only if FD refers to a regular file for
@@ -1201,6 +1201,13 @@ tail_lines (const char *pretty_filename, int fd, uintmax_t n_lines,
}
else
{
+ /* Under very unlikely circumstances, it is possible to reach
+ this point after positioning the file pointer to end of file
+ via the `lseek (...SEEK_END)' above. In that case, reposition
+ the file pointer back to start_pos before calling pipe_lines. */
+ if (start_pos != -1)
+ xlseek (fd, start_pos, SEEK_SET, pretty_filename);
+
return pipe_lines (pretty_filename, fd, n_lines, read_pos);
}
}