git: 1c6b4a0f3eb0 - stable/14 - MFC: tail: Do not trust st_size if it equals zero.

From: Xin LI <delphij_at_FreeBSD.org>
Date: Sun, 22 Dec 2024 07:49:37 UTC
The branch stable/14 has been updated by delphij:

URL: https://cgit.FreeBSD.org/src/commit/?id=1c6b4a0f3eb089e2eba644b604af40d7dbaff47a

commit 1c6b4a0f3eb089e2eba644b604af40d7dbaff47a
Author:     Ricardo Branco <rbranco@suse.de>
AuthorDate: 2024-01-03 20:32:47 +0000
Commit:     Xin LI <delphij@FreeBSD.org>
CommitDate: 2024-12-22 07:49:30 +0000

    MFC: tail: Do not trust st_size if it equals zero.
    
    PR:             bin/276107
    
    (cherry picked from commit 1fb3caee72241b9b4dacbfb0109c972a86d4401f)
---
 usr.bin/tail/forward.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/usr.bin/tail/forward.c b/usr.bin/tail/forward.c
index 2edf8730e8b1..afbff43e6063 100644
--- a/usr.bin/tail/forward.c
+++ b/usr.bin/tail/forward.c
@@ -105,7 +105,7 @@ forward(FILE *fp, const char *fn, enum STYLE style, off_t off, struct stat *sbp)
 	case FBYTES:
 		if (off == 0)
 			break;
-		if (S_ISREG(sbp->st_mode)) {
+		if (S_ISREG(sbp->st_mode) && sbp->st_size > 0) {
 			if (sbp->st_size < off)
 				off = sbp->st_size;
 			if (fseeko(fp, off, SEEK_SET) == -1) {
@@ -137,7 +137,7 @@ forward(FILE *fp, const char *fn, enum STYLE style, off_t off, struct stat *sbp)
 		}
 		break;
 	case RBYTES:
-		if (S_ISREG(sbp->st_mode)) {
+		if (S_ISREG(sbp->st_mode) && sbp->st_size > 0) {
 			if (sbp->st_size >= off &&
 			    fseeko(fp, -off, SEEK_END) == -1) {
 				ierr(fn);
@@ -154,7 +154,7 @@ forward(FILE *fp, const char *fn, enum STYLE style, off_t off, struct stat *sbp)
 				return;
 		break;
 	case RLINES:
-		if (S_ISREG(sbp->st_mode))
+		if (S_ISREG(sbp->st_mode) && sbp->st_size > 0)
 			if (!off) {
 				if (fseeko(fp, (off_t)0, SEEK_END) == -1) {
 					ierr(fn);