Blob Blame History Raw
From 76bfabfffc6d10f3b55c896dc7afc24fa3a71fc9 Mon Sep 17 00:00:00 2001
From: Phil Sutter <psutter@redhat.com>
Date: Wed, 20 Jun 2018 09:22:47 +0200
Subject: [PATCH] erec: Avoid passing negative offset to fseek()

Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1504157
Upstream Status: nftables commit 3570b6bc8b4f1

commit 3570b6bc8b4f136d07121b28cd79b6356e8e969b
Author: Phil Sutter <phil@nwl.cc>
Date:   Thu Mar 1 15:00:28 2018 +0100

    erec: Avoid passing negative offset to fseek()

    If the initial call to ftell() fails, variable orig_offset is set to -1.
    Avoid passing this to fseek() later on.

    Signed-off-by: Phil Sutter <phil@nwl.cc>
    Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
---
 src/erec.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/src/erec.c b/src/erec.c
index 80806ff..8de249d 100644
--- a/src/erec.c
+++ b/src/erec.c
@@ -121,7 +121,7 @@ void erec_print(struct output_ctx *octx, const struct error_record *erec,
 	char buf[1024] = {};
 	char *pbuf = NULL;
 	unsigned int i, end;
-	int l, ret;
+	int l;
 	off_t orig_offset = 0;
 	FILE *f = octx->output_fp;
 
@@ -136,12 +136,12 @@ void erec_print(struct output_ctx *octx, const struct error_record *erec,
 		break;
 	case INDESC_FILE:
 		orig_offset = ftell(indesc->fp);
-		fseek(indesc->fp, loc->line_offset, SEEK_SET);
-		ret = fread(buf, 1, sizeof(buf) - 1, indesc->fp);
-		if (ret > 0)
+		if (orig_offset >= 0 &&
+		    !fseek(indesc->fp, loc->line_offset, SEEK_SET) &&
+		    fread(buf, 1, sizeof(buf) - 1, indesc->fp) > 0 &&
+		    !fseek(indesc->fp, orig_offset, SEEK_SET))
 			*strchrnul(buf, '\n') = '\0';
 		line = buf;
-		fseek(indesc->fp, orig_offset, SEEK_SET);
 		break;
 	case INDESC_INTERNAL:
 	case INDESC_NETLINK:
-- 
1.8.3.1