From 51b91af60bed926023e9c02c0b58b3d91002f382 Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Sat, 20 May 2023 06:51:04 +0100 Subject: [PATCH] tail: Don't read past EOF when reading from a seekable stream Previously, using tail to read from a file would fail, as we would seek to the end of the file then try to read a byte from that position. --- Userland/Utilities/tail.cpp | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Userland/Utilities/tail.cpp b/Userland/Utilities/tail.cpp index 6f89a84ed3..1963771515 100644 --- a/Userland/Utilities/tail.cpp +++ b/Userland/Utilities/tail.cpp @@ -14,7 +14,7 @@ static ErrorOr tail_from_pos(Core::File& file, off_t startline) { - TRY(file.seek(startline + 1, SeekMode::SetPosition)); + TRY(file.seek(startline, SeekMode::SetPosition)); auto buffer = TRY(file.read_until_eof()); out("{}", StringView { buffer }); return {}; @@ -29,11 +29,9 @@ static ErrorOr find_seek_pos(Core::File& file, int wanted_lines) off_t end = pos; int lines = 0; - for (; pos >= 0; pos--) { - TRY(file.seek(pos, SeekMode::SetPosition)); + for (; pos >= 1; pos--) { + TRY(file.seek(pos - 1, SeekMode::SetPosition)); - if (file.is_eof()) - break; auto ch = TRY(file.read_value()); if (ch == '\n' && (end - pos) > 1) { lines++;